Open In App

C# Program to Demonstrate the Use of GetCommandLineArgs() Method of Environment Class

Improve
Improve
Like Article
Like
Save
Share
Report

Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since last system boot in milliseconds information. In this article, we will demonstrate the use of the GetCommandLineArgs() Method of the Environment Class. This method is used to get command-line arguments for the current process and returns an array of strings that contains arguments passed on the command line. The command-line arguments are generally delimited by spaces and we can use double quotation marks (“) to include spaces within an argument. Whereas the single quotation mark (‘), does not give the same functionality. When a double quotation mark follows an even number of backslashes, then the starting pair of backslashes were replaced with the one backslash, and the double quotation marks are also removed. For example: input: MyData \\\\”app1 \\\\”app2, output: MyData \\\app1, \\app2. And when a double quotation mark follows an odd number of backslashes, then the starting pair of backslashes were replaced with the one backslash and the remaining backslashes were removed. In this situation, double quotation marks will not remove. For example: input: MyData \\\\\”app1 \”app2, output: MyData \\”app1, “app2

Syntax:

public static string[] GetCommandLineArgs ()

Return: It returns an array of strings that holds the command-line arguments for the currently running process. In this array, the first item is the executable file name and the remaining items are the command line arguments.

Exception:

NotSupportedException: This exception is thrown when an invoked method is not supported, or when there is an attempt to read, seek, or write to a stream that does not support the invoked functionality.

Example:

Input  : c/cpp java python
Output :
c/cpp
java
python

Input  : 11 java python
Output :
11
java
python

Example:

C#




// C# program to illustrate the use of
// GetCommandLineArgs() Method of Environment Class
using System;
 
class GFG{
     
static public void Main()
{
     
    // Declare an array of string that holds the
    // command-line arguments for the running process
    // Using GetCommandLineArgs() method of
    // Environment class
    string[] commandline = Environment.GetCommandLineArgs();
 
    Console.WriteLine("Command Line Arguments are:");
     
    // Display the command-line arguments for
    // the current process
    foreach(string arguments in commandline)
    {
        Console.WriteLine("\t" + arguments);
    }
}
}


Output:

Command Line Arguments are:
/Users/Projects/newprogram/newprogram/bin/Debug/netcoreapp3.0/newprogram.dll

Last Updated : 17 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads