---
title: "How to Pass or Access Command-line Arguments in C#?"  
description: "How to Pass or Access Command-line Arguments in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/forum/160704/how-to-pass-or-access-command-line-arguments-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# How to Pass or Access Command-line Arguments in C#?

How to Pass or [Access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) [Command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)-[line Arguments](https://www.mindstick.com/forum/158085/how-do-i-pass-command-line-arguments-to-a-node-js-program) in C#?

## Replies

### Reply by Ravi Vishwakarma

In C#, you can pass command-line [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) to your application and access them within the Main method. The Main method can be defined to accept an array of strings as its parameter, which represents the command-line arguments.

**Example of Passing and Accessing Command-line Arguments**\
Here's a simple example demonstrating how to pass and access command-line arguments in a C# console application:

```cs
using System;
class Program
{
   // Main method accepting command-line arguments
   static void Main(string[] args)
   {
       // Check if arguments are provided
       if (args.Length > 0)
       {
           // Loop through the arguments and print them
           for (int i = 0; i < args.Length; i++)
           {
               Console.WriteLine($"Argument {i}: {args[i]}");
           }
       }
       else
       {
           Console.WriteLine("No command-line arguments were provided.");
       }
   }
}
```

\
**Running the Application with Command-line Arguments**\
To run the application with command-line arguments, you can use a terminal or command prompt. Navigate to the directory where your compiled executable is located and run it with arguments:

```plaintext
dotnet run arg1 arg2 arg3
```

Or if you are running the compiled executable directly:

```plaintext
Program.exe arg1 arg2 arg3
```

#### \
Handling Command-line Arguments in Visual Studio

If you are using Visual Studio, you can set command-line arguments in the project properties:

1. Right-click on the project in Solution Explorer and select "**Properties**".
2. Go to the "**Debug**" tab.
3. In the "**Command line arguments**" field, enter the arguments you want to pass to the application.
4. Save the changes and run the project.

![How to Pass or Access Command-line Arguments in C#?](https://www.mindstick.com/mindstickforums/e9938ba2-1093-4854-8960-1d361485a615/images/2de104a3-ff21-4444-a111-7f3b954ba0b8.png)

## Detailed Explanation

1. **Defining the Main Method:** The Main method is defined with a parameter args, which is an array of strings (string[] args). This array holds the command-line arguments passed to the application.
2. **Checking for Arguments:** The code checks if any arguments are provided by checking the length of the args array (args.Length).
3. **Looping Through the Arguments:** If arguments are provided, a for loop is used to iterate through the args array and print each argument along with its index.
4. **No Arguments Provided:** If no arguments are provided, a message is printed indicating that no command-line arguments were given.

## Output :

![How to Pass or Access Command-line Arguments in C#?](https://www.mindstick.com/mindstickforums/e9938ba2-1093-4854-8960-1d361485a615/images/a305b0fb-9387-4b6b-a0e3-9c835a0a3353.png)


---

Original Source: https://www.mindstick.com/forum/160704/how-to-pass-or-access-command-line-arguments-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
