GetCommandLineArgs Method

Environment.GetCommandLineArgs Method

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

[Visual Basic]
Public Shared Function GetCommandLineArgs() As String()
[C#]
public static string[] GetCommandLineArgs();
[C++]
public: static String* GetCommandLineArgs()  __gc[];
[JScript]
public static function GetCommandLineArgs() : String[];

Return Value

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.

Exceptions

Exception Type Condition
NotSupportedException The system does not support command line arguments.

Remarks

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.

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.

Example

[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
*/

[C++] 
// Sample for the Environment::GetCommandLineArgs method
#using <mscorlib.dll>

using namespace System;

int main() {
   Console::WriteLine();
   //  Invoke this sample with an arbitrary set of command line arguments.
   String*  arguments[] = Environment::GetCommandLineArgs();
   Console::WriteLine(S"GetCommandLineArgs: {0}", String::Join(S", ", arguments));
}
/*
This example produces the following results:

C:\>GetCommandLineArgs ARBITRARY TEXT

GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Environment Class | Environment Members | System Namespace | CommandLine

Show:
© 2016 Microsoft