Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System
Environment Class
Environment Methods
 GetCommandLineArgs Method
Collapse All/Expand All Collapse All
.NET Framework Class Library
Environment..::.GetCommandLineArgs Method

Returns a string array containing the command-line arguments for the current process.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Shared Function GetCommandLineArgs As String()
C#
public static string[] GetCommandLineArgs()
Visual C++
public:
static array<String^>^ GetCommandLineArgs()
F#
static member GetCommandLineArgs : unit -> string[] 

Return Value

Type: array<System..::.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.
ExceptionCondition
NotSupportedException

The system does not support command-line arguments.

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.

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, \\bet

MyApp \\\\\"alpha \"beta

MyApp, \\"alpha, "beta

To obtain the command line as a single string, use the CommandLine property.

Windows NT 4.0, Windows 2000 Platform Note: The name of the executable file does not include the path.

Windows 98, Windows Millennium Edition Platform Note: The name of the executable file includes the path. Long file names (non-8dot3 names) can be shortened to their 8dot3 representation.

The following example shows displays the application's command line arguments.

Visual Basic
' Sample for the Environment.GetCommandLineArgs method
Imports System

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 'Main
End Class 'Sample
'
'This example produces the following results:
'
'C:\>GetCommandLineArgs ARBITRARY TEXT
'
'GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
'
C#
// Sample for the Environment.GetCommandLineArgs method
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 the following results:

C:\>GetCommandLineArgs ARBITRARY TEXT

GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
Visual C++
// Sample for the Environment::GetCommandLineArgs method
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 the following results:

C:\>GetCommandLineArgs ARBITRARY TEXT

GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Typo?      xkgf   |   Edit   |   Show History

The fourth example is inconsistent

MyApp \\\alpha \\\\"beta

is mapped to

MyApp, \\\alpha, \\bet

I think the final "a" is missing.

Tags What's this?: Add a tag
Flag as ContentBug
Documentation Error or ???      BillJam   |   Edit   |   Show History
If you use CreateProcessWithLogonW (maybe others like ShellExec) to launch another executable, the first string in the array is not the program name but the first argument specified. If no arguments are specified, the program name as the first an only string. Environment.CommandLine does the same thing.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker