Environment.GetCommandLineArgs Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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()
An array of strings 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
*/
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
'
The first element in the array contains the file name of the executing program. If 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.
In .NET 5 and later versions, for single-file publishing, the first element is the name of the host executable.
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.
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 |
To obtain the command line as a single string, use the CommandLine property.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.5, 1.6, 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: