ProcessStartInfo.Arguments 屬性

定義

取得或設定啟動應用程式時要使用的命令列引數集。

public:
 property System::String ^ Arguments { System::String ^ get(); void set(System::String ^ value); };
public string Arguments { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Arguments { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Arguments { get; set; }
[System.ComponentModel.SettingsBindable(true)]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Arguments { get; set; }
[System.ComponentModel.SettingsBindable(true)]
public string Arguments { get; set; }
member this.Arguments : string with get, set
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Arguments : string with get, set
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Arguments : string with get, set
[<System.ComponentModel.SettingsBindable(true)>]
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Arguments : string with get, set
[<System.ComponentModel.SettingsBindable(true)>]
member this.Arguments : string with get, set
Public Property Arguments As String

屬性值

單一字串,包含要傳遞至 FileName 屬性中所指定之目標應用程式的引數。 預設為空字串 ("")。

屬性

範例

第一個範例會建立小型應用程式 (argsecho.exe) ,以回應其自變數至主控台。 第二個範例會建立叫用 argsecho.exe 的應用程式,以示範屬性的不同變化 Arguments

// Place this code into a console project called ArgsEcho to build the argsecho.exe target

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine("Received the following arguments:\n");

    for (int i = 0; i < args->Length; i++)
    {
        Console::WriteLine("[" + i + "] = " + args[i]);
    }

    Console::WriteLine("\nPress any key to exit");
    Console::ReadLine();
    return 0;
}
// Place this code into a console project called ArgsEcho to build the argsecho.exe target

using System;

namespace StartArgs
{
    class ArgsEcho
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Received the following arguments:\n");

            for (var i = 0; i < args.Length; i++)
            {
                Console.WriteLine($"[{i}] = {args[i]}");
            }

            Console.WriteLine("\nPress any key to exit");
            Console.ReadLine();
        }
    }
}
' Place this code into a console project called ArgsEcho to build the argsecho.exe target

Module Module1
    Sub Main()
        Dim i As Integer = 0

        For Each s As String In My.Application.CommandLineArgs
            Console.WriteLine($"[{i}] = {s}")
            i = i + 1
        Next

        Console.WriteLine(Environment.NewLine + "Press any key to exit")
        Console.ReadLine()
    End Sub
End Module
// Place the following code into a console project called StartArgsEcho. It depends on the
// console application named argsecho.exe.

using namespace System;
using namespace System::Diagnostics;

int main()
{
    ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("argsecho.exe");
    startInfo->WindowStyle = ProcessWindowStyle::Normal;

    // Start with one argument.
    // Output of ArgsEcho:
    //  [0]=/a            
    startInfo->Arguments = "/a";
    Process::Start(startInfo);

    // Start with multiple arguments separated by spaces.
    // Output of ArgsEcho:
    //  [0] = /a
    //  [1] = /b
    //  [2] = c:\temp
    startInfo->Arguments = "/a /b c:\\temp";
    Process::Start(startInfo);

    // An argument with spaces inside quotes is interpreted as multiple arguments.
    // Output of ArgsEcho:
    //  [0] = /a
    //  [1] = literal string arg
    startInfo->Arguments = "/a \"literal string arg\"";
    Process::Start(startInfo);

    // An argument inside double quotes is interpreted as if the quote weren't there,
    // that is, as separate arguments. 
    // Output of ArgsEcho:
    //  [0] = /a
    //  [1] = /b:string
    //  [2] = in
    //  [3] = double
    //  [4] = quotes
    startInfo->Arguments = "/a /b:\"\"string in double quotes\"\"";
    Process::Start(startInfo);

    // Triple-escape quotation marks to include the character in the final argument received
    // by the target process.
    //  [0] = /a
    //  [1] = /b:"quoted string"
    startInfo->Arguments = "/a /b:\"\"\"quoted string\"\"\"";
    Process::Start(startInfo);

    return 0;
}
// Place this code into a console project called StartArgsEcho. It depends on the
// console application named argsecho.exe.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace StartArgsEcho
{
    class Program
    {
        static void Main()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Normal;

            // Start with one argument.
            // Output of ArgsEcho:
            //  [0]=/a
            startInfo.Arguments = "/a";
            Process.Start(startInfo);

            // Start with multiple arguments separated by spaces.
            // Output of ArgsEcho:
            //  [0] = /a
            //  [1] = /b
            //  [2] = c:\temp
            startInfo.Arguments = "/a /b c:\\temp";
            Process.Start(startInfo);

            // An argument with spaces inside quotes is interpreted as multiple arguments.
            // Output of ArgsEcho:
            //  [0] = /a
            //  [1] = literal string arg
            startInfo.Arguments = "/a \"literal string arg\"";
            Process.Start(startInfo);

            // An argument inside double quotes is interpreted as if the quote weren't there,
            // that is, as separate arguments. Equivalent verbatim string is @"/a /b:""string with quotes"""
            // Output of ArgsEcho:
            //  [0] = /a
            //  [1] = /b:string
            //  [2] = in
            //  [3] = double
            //  [4] = quotes
            startInfo.Arguments = "/a /b:\"\"string in double quotes\"\"";
            Process.Start(startInfo);

            // Triple-escape quotation marks to include the character in the final argument received
            // by the target process. Equivalent verbatim string: @"/a /b:""""""quoted string""""""";
            //  [0] = /a
            //  [1] = /b:"quoted string"
            startInfo.Arguments = "/a /b:\"\"\"quoted string\"\"\"";
            Process.Start(startInfo);
        }
    }
}
' Place this code into a console project called StartArgsEcho. It depends on the
' console application named argsecho.exe.

Module Module1
    Sub Main()
        Dim startInfo As ProcessStartInfo = New ProcessStartInfo("argsecho.exe")
        startInfo.WindowStyle = ProcessWindowStyle.Normal

        ' Start with one argument.
        ' Output of ArgsEcho:
        '  [0]=/a            
        startInfo.Arguments = "/a"
        Process.Start(startInfo)

        ' Start with multiple arguments separated by spaces.
        ' Output of ArgsEcho:
        '  [0] = /a
        '  [1] = /b
        '  [2] = c:\temp
        startInfo.Arguments = "/a /b c:\temp"
        Process.Start(startInfo)

        ' An argument with spaces inside quotes is interpreted as multiple arguments.
        ' Output of ArgsEcho:
        '  [0] = /a
        '  [1] = literal string arg
        startInfo.Arguments = "/a ""literal string arg"" "
        Process.Start(startInfo)

        ' An argument inside double quotes is interpreted as if the quote weren't there,
        ' that is, as separate arguments.
        ' Output of ArgsEcho:
        '  [0] = /a
        '  [1] = /b:string
        '  [2] = in
        '  [3] = double
        '  [4] = quotes
        startInfo.Arguments = "/a /b:""""string in double quotes"""" "
        Process.Start(startInfo)

        ' Triple-escape quotation marks to include the character in the final argument received
        ' by the target process. 
        '  [0] = /a
        '  [1] = /b:"quoted string"
        startInfo.Arguments = "/a /b:""""""quoted string"""""" "
        Process.Start(startInfo)
    End Sub
End Module

備註

指派給 Arguments 屬性的字串長度必須小於32,699。

引數會由目標應用程式剖析及解譯,因此必須配合該應用程式的期望。 如下列範例所示的 .NET 應用程式,空格會解譯為多個自變數之間的分隔符。 您必須以引號括住含有空格的單一引數,但這些引號不會傳送至目標應用程式。 若要在最終剖析自變數中包含引號,請三次逸出每個標記。 如果您使用這個屬性來設定命令行自變數, ArgumentList 則不得包含任何專案。

ArgumentsArgumentList,從 .NET Core 2.1 和 .NET Standard 2.1 開始支援,彼此無關。 也就是說,指派給 屬性的 Arguments 字串不會填 ArgumentList 入集合,而且不會將集合的成員 ArgumentList 指派給 Arguments 屬性。

重要

使用此物件的執行個體時,若並用了不信任的資料,會造成安全性上的風險。 使用此物件時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

適用於