How to Pass and Access Command-line Arguments in C#: A Beginner's Guide

Category > CSHARP || Published on : Monday, March 13, 2023 || Views: 347 || C# command-line arguments beginner's guide programming tutorial development.


Learn how to pass and access command-line arguments in your C# programs with ease. This article explains what command-line arguments are, how to pass them to a program, and how to access them in C# using the args parameter in the Main method. It also provides examples of how to work with command-line arguments in your program.

When working on a C# project, it's common to need to pass command-line arguments to the program. These arguments provide a way to pass information into the program without the need for user input. In this article, we will discuss how to pass and access command-line arguments in C#.

What are Command-line Arguments?

Command-line arguments are strings of information passed to a program when it is executed in the command-line interface. These arguments can be used to modify the behavior of the program or provide input data for processing.

How to Pass Command-line Arguments in C#

In C#, we can pass command-line arguments by specifying them after the name of the program when we run it from the command-line interface. For example, if we have a program called "myprogram.exe" and we want to pass two arguments "arg1" and "arg2" to it, we can run it as follows:

myprogram.exe arg1 arg2

How to Access Command-line Arguments in C#

To access the command-line arguments in C#, we can use the args parameter in the Main method. The args parameter is an array of strings that contains the command-line arguments passed to the program.

Here's an example of how to access command-line arguments in C#:

static void Main(string[] args)
{
    // Print out the command-line arguments
    foreach (string arg in args)
    {
        Console.WriteLine(arg);
    }
}

In this example, we're using a foreach loop to iterate over the args array and print out each argument to the console.

Working with Command-line Arguments

Once we have access to the command-line arguments, we can use them in our program as needed. For example, we could use the arguments to specify a file to read, a database connection string, or any other configuration data needed by our program.

Here's an example of how to use command-line arguments to read a file:

static void Main(string[] args)
{
    // Check if the user passed in a filename
    if (args.Length > 0)
    {
        // Read the file contents
        string fileContents = File.ReadAllText(args[0]);

        // Print out the file contents
        Console.WriteLine(fileContents);
    }
    else
    {
        Console.WriteLine("Please provide a filename as a command-line argument.");
    }
}

In this example, we're checking if the user passed in a filename as a command-line argument. If they did, we're reading the file contents and printing them to the console. If they didn't pass in a filename, we're printing a message asking them to provide one.

Conclusion

In conclusion, passing and accessing command-line arguments in C# is a simple process. We just need to specify the arguments after the name of the program when we run it from the command-line interface, and then access them using the args parameter in the Main method. Once we have access to the command-line arguments, we can use them in our program as needed.