Environment.GetCommandLineArgs 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳包含當前程序命令列參數的字串陣列。
public:
static cli::array <System::String ^> ^ GetCommandLineArgs();
public static string[] GetCommandLineArgs();
static member GetCommandLineArgs : unit -> string[]
Public Shared Function GetCommandLineArgs () As String()
傳回
一個字串陣列,每個元素都包含一個命令列參數。 第一個元素是可執行檔名稱,接下來的零個或多個元素包含剩餘的命令列參數。
例外狀況
系統不支援命令列參數。
範例
以下範例顯示應用程式的命令列參數。
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
*/
open System
// Invoke this sample with an arbitrary set of command line arguments.
let arguments = Environment.GetCommandLineArgs()
String.concat ", " arguments
|> printfn "\nGetCommandLineArgs: %s"
// 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
'
備註
陣列中的第一個元素包含執行程式的檔案名稱。 若檔名無法取得,第一個元素等 String.Empty於 。 剩餘元素則包含命令列中輸入的任何額外標記。
在 .NET 5 及之後版本中,單檔案發佈的第一個元素是主機執行檔的名稱。
程式檔名可以(但不強制)包含路徑資訊。
命令列參數以空格為界定。 你可以使用雙引號(“)來在論元中包含空格。 然而,單引號(')並不提供此功能。
如果雙引號跟在兩個或偶數個反斜線後面,接下來的每一對反斜線都會被一個反斜線取代,雙引號也會被移除。 如果雙引號跟隨奇數個反斜線,其中只有一個,則前方每對引號會被替換為一個反斜線,剩餘的反斜線則被移除;但此處雙引號並未被移除。
下表展示了命令列參數的界定方式,並假設 MyApp 目前執行的應用程式是
| 指令列輸入 | 產生的命令列參數 |
|---|---|
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 。