Environment.GetCommandLineArgs Environment.GetCommandLineArgs Environment.GetCommandLineArgs Environment.GetCommandLineArgs Method

定義

傳回字串陣列,包含目前處理序的命令列引數。Returns a string array containing the command-line arguments for the current process.

public:
 static cli::array <System::String ^> ^ GetCommandLineArgs();
public static string[] GetCommandLineArgs ();
static member GetCommandLineArgs : unit -> string[]
Public Shared Function GetCommandLineArgs () As String()

傳回

String[]

字串的陣列,其中的每個元素都包含命令列引數。An array of string where each element contains a command-line argument. 第一個元素是可執行檔名稱,後面的零或多個元素包含其餘的命令列引數。The first element is the executable file name, and the following zero or more elements contain the remaining command-line arguments.

例外狀況

系統不支援命令列引數。The system does not support command-line arguments.

範例

下列範例會顯示應用程式的命令列引數。The following example displays the application's command line arguments.

using namespace System;

int main()
{
   Console::WriteLine();
   
   //  Invoke this sample with an arbitrary set of command line arguments.
   array<String^>^ arguments = Environment::GetCommandLineArgs();
   Console::WriteLine( "GetCommandLineArgs: {0}", String::Join( ", ", arguments ) );
}
/*
This example produces output like the following:
    
    C:\>GetCommandLineArgs ARBITRARY TEXT
    
      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    //  Invoke this sample with an arbitrary set of command line arguments.
    String[] arguments = Environment.GetCommandLineArgs();
    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
    }
}
/*
This example produces output like the following:
    
    C:\>GetCommandLineArgs ARBITRARY TEXT
    
      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      '  Invoke this sample with an arbitrary set of command line arguments.
      Dim arguments As String() = Environment.GetCommandLineArgs()
      Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments))
   End Sub
End Class
'This example produces output like the following:
'    
'    C:\>GetCommandLineArgs ARBITRARY TEXT
'    
'      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
'    

備註

陣列中的第一個項目包含的檔案名稱的執行中的程式。The first element in the array contains the file name of the executing program. 如果無法使用的檔案名稱,第一個項目是否等於String.EmptyIf the file name is not available, the first element is equal to String.Empty. 其餘的項目包含在命令列上輸入的任何其他權杖。The remaining elements contain any additional tokens entered on the command line.

程式檔案名稱,但不是需要,包含路徑資訊。The program file name can, but is not required to, include path information.

命令列引數是以空格分隔。Command line arguments are delimited by spaces. 您可以使用雙引號 ("),以引數中加入空格。You can use double quotation marks (") to include spaces within an argument. 不過,單引號 ('),不提供這項功能。The single quotation mark ('), however, does not provide this functionality.

如果雙引號後面兩個或偶數數目的反斜線,每個繼續反斜線配對會取代一個反斜線,並移除雙引號。If a double quotation mark follows two or an even number of backslashes, each proceeding backslash pair is replaced with one backslash and the double quotation mark is removed. 如果雙引號後面奇數數目的反斜線,包括只執行一個一個反斜線取代每個先前的配對,並移除其餘的反斜線;不過,在此情況下將雙引號不會移除。If a double quotation mark follows an odd number of backslashes, including just one, each preceding pair is replaced with one backslash and the remaining backslash is removed; however, in this case the double quotation mark is not removed.

下表顯示如何命令列引數可以是分隔,並假設MyApp為目前正在執行的應用程式。The following table shows how command line arguments can be delimited, and assumes MyApp as the current executing application.

在命令列輸入Input at the command line 產生的命令列引數Resulting command line arguments
MyApp alpha beta MyApp, alpha, beta
MyApp "alpha with spaces" "beta with spaces" MyApp, alpha with spaces, beta with spaces
MyApp 'alpha with spaces' beta MyApp, 'alpha, with, spaces', beta
MyApp \\\alpha \\\\"beta MyApp, \\\alpha, \\beta
MyApp \\\\\"alpha \"beta MyApp, \\"alpha, "beta

若要取得命令列以單一字串,請使用CommandLine屬性。To obtain the command line as a single string, use the CommandLine property.

安全性

EnvironmentPermission
針對至 PATH 環境變數的讀取權限。for read access to the PATH environment variable. 相關聯的列舉型別:Associated enumeration: Read

適用於

另請參閱