C# Program to Get and Print the Command Line Arguments Using Environment Class

The Environment class in C# provides access to current environment and platform information, including command line arguments. The Environment.CommandLine property retrieves the complete command line that started the current process, including the executable name and all arguments.

Environment Class Overview

The Environment class offers various properties and methods to access system information

  • Environment.CommandLine Gets the complete command line

  • Environment.GetCommandLineArgs() Gets arguments as a string array

  • Environment.CurrentDirectory Gets current working directory

  • Environment.OSVersion Gets operating system version

  • Environment.MachineName Gets computer name

  • Environment.ProcessorCount Gets processor count

Command Line Processing myapp.exe arg1 "arg with spaces" arg3 CommandLine Returns complete command as string GetCommandLineArgs() Returns arguments as string array

Syntax

Following is the syntax for accessing command line arguments using Environment class

// Get complete command line as string
string commandLine = Environment.CommandLine;

// Get arguments as string array
string[] args = Environment.GetCommandLineArgs();

Using Environment.CommandLine Property

The CommandLine property returns the entire command line including the executable name

using System;

class Program {
   static void Main() {
      // Get complete command line
      string commandLine = Environment.CommandLine;
      
      // Display the command line
      Console.WriteLine("Complete Command Line:");
      Console.WriteLine(commandLine);
      
      // Display some additional environment info
      Console.WriteLine("\nExecutable Path:");
      Console.WriteLine(Environment.GetCommandLineArgs()[0]);
      
      Console.WriteLine("\nCurrent Directory:");
      Console.WriteLine(Environment.CurrentDirectory);
   }
}

When executed from command line as myapp.exe hello world, the output would be

Complete Command Line:
myapp.exe hello world

Executable Path:
myapp.exe

Current Directory:
C:\MyProgram

Using Environment.GetCommandLineArgs() Method

The GetCommandLineArgs() method returns arguments as a string array, making it easier to process individual arguments

using System;

class CommandLineProcessor {
   static void Main() {
      // Get command line arguments as array
      string[] args = Environment.GetCommandLineArgs();
      
      Console.WriteLine("Total arguments: " + args.Length);
      Console.WriteLine("\nArguments breakdown:");
      
      for (int i = 0; i < args.Length; i++) {
         if (i == 0) {
            Console.WriteLine("Program: " + args[i]);
         } else {
            Console.WriteLine("Arg " + i + ": " + args[i]);
         }
      }
      
      // Process arguments if any were provided
      if (args.Length > 1) {
         Console.WriteLine("\nProcessing arguments:");
         for (int i = 1; i < args.Length; i++) {
            Console.WriteLine("Processing: " + args[i]);
         }
      } else {
         Console.WriteLine("\nNo arguments provided.");
      }
   }
}

When executed from command line as processor.exe file1.txt file2.txt -verbose, the output would be

Total arguments: 4

Arguments breakdown:
Program: processor.exe
Arg 1: file1.txt
Arg 2: file2.txt
Arg 3: -verbose

Processing arguments:
Processing: file1.txt
Processing: file2.txt
Processing: -verbose

Comparison of Approaches

Property/Method Return Type Description Use Case
Environment.CommandLine string Complete command line as single string Logging, debugging, display purposes
Environment.GetCommandLineArgs() string[] Arguments separated into array elements Processing individual arguments
Main(string[] args) string[] Arguments passed to Main method Standard argument processing

Practical Example with Argument Processing

using System;

class FileProcessor {
   static void Main() {
      string[] args = Environment.GetCommandLineArgs();
      
      Console.WriteLine("Command Line: " + Environment.CommandLine);
      Console.WriteLine("Arguments Count: " + (args.Length - 1));
      
      // Skip first argument (program name) and process others
      for (int i = 1; i < args.Length; i++) {
         string arg = args[i];
         
         if (arg.StartsWith("-")) {
            Console.WriteLine("Option found: " + arg);
         } else if (arg.EndsWith(".txt")) {
            Console.WriteLine("Text file found: " + arg);
         } else {
            Console.WriteLine("Other argument: " + arg);
         }
      }
      
      if (args.Length == 1) {
         Console.WriteLine("Usage: program.exe [options] [files]");
      }
   }
}

When executed as program.exe -debug file1.txt -output result.txt, the output would be

Command Line: program.exe -debug file1.txt -output result.txt
Arguments Count: 4
Option found: -debug
Text file found: file1.txt
Option found: -output
Text file found: result.txt

Conclusion

The Environment class in C# provides convenient access to command line arguments through CommandLine property and GetCommandLineArgs() method. Use CommandLine for displaying the complete command line and GetCommandLineArgs() for processing individual arguments in your applications.

Updated on: 2026-03-17T07:04:36+05:30

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements