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


Introduction

Let us see how to use the instrumental environment class of C# to write a C# Program to Get and Print the Command Line Arguments Using Environment Class. Knowing all the things about C#, we will now understand one of the uses of the system.environment class in C# and then we will learn to code a program that will get and print the command line arguments. Basically, it will take strings as arguments and will have its return type as a string. Before diving into the program we must have an overview of what is environment class in detail so let us learn that.

What is Environment Class in C#?

Getting information about a class with an understanding of the literal meaning of the name of the class can be very logical unless you are learning bootstrap! because when it comes to bootstrap they really have an ugly way of assigning the name of their class but that is not the case in the learning of C#. The C# Environment class performs exactly the information you interpret with its name. It helps in knowing about the current environment and also allows us to modify the current platform, it also provides information about the various operating systems related information.

The other uses of the environment class include information about the count of the processors, the name of the computer network, the version of the operating system being used, the name of the current user, and the current directory.

The environment class in C# consists of various functions and properties to accomplish the various utilities as described above, like the following

  • Environment.CommandLine

  • Environment.CurrentDirectory

  • Environment.OSVersion.ToString()

  • Environment.MachineName.ToString()

  • Environment.ProcessorCount.ToString()

In this article, we will be learning about the Environment.CommandLine functions in detail to get and print the command line arguments, so let us understand the problem statement with the help of an example.

Algorithm

The algorithm below will give a total understanding of the code to get and display the command line arguments using the environment class. We will know the step-by-step methods to have a deeper understanding of the code.

Step 1 − Create a class named Tutotrialspoint.

Step 2  Since the return type of the CommandLine() function is a string, let us declare a variable of string data type to catch the value.

Step 3  Use the Environment.CommandLine() function to store the argument in the abovecreated variable of string data type.

Step 4  Display the data using the standard Console.WriteLine() function.

This algorithm will ease you to write the proper code for the above problem statement, let us now have a look at its code.

Example

Let us suppose a user executes the .exe file on the command line and wishes to send the arguments as “This is a demo text”, then our program must display the output as “This is a demo text”. Let us understand the approach for the program to perform the above function.

// A program to get and print the command line arguments

// with the help of Environment Class using C#
using System;
class TutotrialsPoint{
   static public void Main() {

      //Declare a variable of string data type to hold the value of arguments
      string Result = “”;
      /* With the help of CommandLine property accessing the command line arguments passed by the users. */
      Result = Environment.CommandLine;

      // Printing the argument
      Console.WriteLine("Command Line Arguments: 
" +Result); } }

Output

E:\> example.exe This is a demo text
Command Line Arguments:
example.exe This is a demo text

Note − This code will run successfully on your compiler but it will give desired output only when it is executed through the command line with suitable arguments passed by the users. On any regular compiler, it won’t show any output because of the insufficiency of any argument provided by the user.

Time Complexity

The above program only includes a pre-defined function named Environment.CommandLine() which is a hardcore read-only type of function in C#, we cannot understand its internal working, and thus the time complexity is indeterminable in this case.

Conclusion

So quick to reach here, was not it? In this article, we learned about the environment class in C#. We saw multiple utilities of the system.environment class like providing information about the versions of the operating system, name of the directory, and also the information about the current platform, but we emphasized the environment.CommandLine() function to retrieve the arguments passed by the user on the command line

We saw the algorithm to write the code and it was followed by the working code for accessing and displaying the arguments provided by the user, and at the end of the article we had a discussion on the time complexity of the problem. So with this, we end the article. We hope that this article enhances your knowledge regarding C#.

Updated on: 21-Apr-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements