C# - Environment.GetCommandLineArgs() Method with Example

In this tutorial, we will learn about the C# Environment.GetCommandLineArgs() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 30, 2023

Environment.GetCommandLineArgs() Method

The Environment.GetCommandLineArgs() method returns an array of strings that contains arguments passed on the command line.

Syntax

string [] Environment.GetCommandLineArgs();

Parameter(s)

This does not accept any parameter.

Return Value

This method returns an array of strings that contains arguments passed on the command line.

Exception(s)

  • System.NotSupportedException

C# Example of Environment.GetCommandLineArgs() Method

The source code to demonstrate the use of GetCommandLineArgs() method of Environment class is given below. The given program is compiled and executed successfully.

using System;

class Sample
{
    //Entry point of Program
    static public void Main()
    {
        string[] cmdArg;

        cmdArg = Environment.GetCommandLineArgs();

        Console.WriteLine("Command Line Arguments:");
        foreach (string arg in cmdArg)
        {
            Console.WriteLine("\t"+arg);
        }
    }
}

Here, we execute the above program using a command prompt to see all arguments passed on the command line.

Output

D:\>Program.exe 123 ABCD
Command Line Arguments:
        Program.exe
        123
        ABCD

C# Environment Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.