Home > Backend Development > C#.Net Tutorial > C# program to get and print command line arguments using environment classes

C# program to get and print command line arguments using environment classes

PHPz
Release: 2023-09-09 08:29:02
forward
845 people have browsed it

使用环境类获取和打印命令行参数的 C# 程序

Introduction

Let’s see how to write a C# program using C#’s tool environment classes to get and print command line arguments using environment classes. After knowing all about C#, we will now understand one of the uses of system.environment class in C# and then we will learn to write a program that gets and prints command line arguments. Basically it will accept a string as parameter and give its return type as string. Before we dive into the program, we have to understand in detail what environment classes are, so let’s learn about them.

What is the environment class in

C#?

Unless you are learning bootstrap, it is probably very logical to get information about a class by understanding the literal meaning of its name! Because when it comes to bootstrap, they do have an ugly way of assigning names to classes, but that's not the case when learning C#. C# environment classes do exactly what you explain with their names. It helps to understand the current environment and allows us to modify the current platform. It also provides relevant information about various operating systems.

Other uses of the environment class include information about the number of processors, computer network name, operating system version in use, current user name, and current directory.

The environment class in C# consists of various functions and properties used to complete various utilities as mentioned above, as shown below

  • Environment.CommandLine

  • Environment.CurrentDirectory

  • Environment.OSVersion.ToString()

  • Environment.MachineName.ToString()

  • Environment.ProcessorCount.ToString()

In this article, we will know in detail the Environment.CommandLine function to get and print command line parameters, so let us understand the problem statement with the help of an example.

algorithm

The algorithm below will give you a comprehensive understanding of the code that uses environment classes to obtain and display command line parameters. We will look at the step-by-step approach to gain a deeper understanding of the code.

Step 1- Create a class called Tutotorialspoint.

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

Step 3 Use the Environment.CommandLine() function to store the parameters in the string data type variable created above.

Step 4 Use the standard Console.WriteLine() function to display the data.

This algorithm will help you easily write the correct code for the above problem statement, now let’s take a look at its code.

Example

Suppose the user executes the .exe file on the command line and wants to send the parameters as "This is demo text", then our program must display the output as "This is demo text" ". Let’s understand how the program performs the above functions.

// 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);
   }
}
Copy after login

Output

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

NOTE - This code will run successfully on your compiler, but it will give the desired output only when executed via the command line with appropriate arguments passed by the user. On any regular compiler it will not show any output due to insufficient arguments provided by the user.

time complexity

The above program only contains a predefined function called Environment.CommandLine(), which is a hardcore read-only type function in C# and we cannot understand its inner workings, so in this case the time complexity It is impossible to determine.

in conclusion

We got here so quickly, didn’t we? In this article, we learned about environment classes in C#. We saw multiple utilities of the system.environment class, such as providing information about the operating system version, directory name, and information about the current platform, but we highlighted the environment.CommandLine() function to retrieve the parameters passed by the user on the command line implement

We saw the algorithm for writing the code, then the working code for accessing and displaying user-supplied parameters, and at the end of the article we discussed the time complexity of the problem. So our article ends here. We hope this article has enhanced your understanding of C#.

The above is the detailed content of C# program to get and print command line arguments using environment classes. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template