What is “string[] args” in Main class for?

后端 未结 8 1496
眼角桃花
眼角桃花 2020-12-13 01:37

In C# the Main class has string[] args parameter.

What is that for and where does it get used?

相关标签:
8条回答
  • 2020-12-13 02:03

    From the C# programming guide on MSDN:

    The parameter of the Main method is a String array that represents the command-line arguments

    So, if I had a program (MyApp.exe) like this:

    class Program
    {
      static void Main(string[] args)
      {
        foreach (var arg in args)
        {
          Console.WriteLine(arg);
        }
      }
    }

    That I started at the command line like this:

    MyApp.exe Arg1 Arg2 Arg3

    The Main method would be passed an array that contained three strings: "Arg1", "Arg2", "Arg3".

    If you need to pass an argument that contains a space then wrap it in quotes. For example:

    MyApp.exe "Arg 1" "Arg 2" "Arg 3"

    Command line arguments commonly get used when you need to pass information to your application at runtime. For example if you were writing a program that copies a file from one location to another you would probably pass the two locations as command line arguments. For example:

    Copy.exe C:\file1.txt C:\file2.txt
    0 讨论(0)
  • 2020-12-13 02:04

    This is an array of the command line switches pass to the program. E.g. if you start the program with the command "myapp.exe -c -d" then string[] args[] will contain the strings "-c" and "-d".

    0 讨论(0)
  • 2020-12-13 02:06

    Besides the other answers. You should notice these args can give you the file path that was dragged and dropped on the .exe file. i.e if you drag and drop any file on your .exe file then the application will be launched and the arg[0] will contain the file path that was dropped onto it.

    static void Main(string[] args)
    {
        Console.WriteLine(args[0]);
    }
    

    this will print the path of the file dropped on the .exe file. e.g

    C:\Users\ABCXYZ\source\repos\ConsoleTest\ConsoleTest\bin\Debug\ConsoleTest.pdb

    Hence, looping through the args array will give you the path of all the files that were selected and dragged and dropped onto the .exe file of your console app. See:

    static void Main(string[] args)
    {
        foreach (var arg in args)
        {
            Console.WriteLine(arg);
        }
        Console.ReadLine();
    }
    

    The code sample above will print all the file names that were dragged and dropped onto it, See I am dragging 5 files onto my ConsoleTest.exe app.

    And here is the output that I get after that:

    0 讨论(0)
  • 2020-12-13 02:14

    You must have seen some application that run from the commandline and let you to pass them arguments. If you write one such app in C#, the array args serves as the collection of the said arguments.

    This how you process them:

    static void Main(string[] args) {
        foreach (string arg in args) {
           //Do something with each argument
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:16

    The args parameter stores all command line arguments which are given by the user when you run the program.

    If you run your program from the console like this:

    program.exe there are 4 parameters

    Your args parameter will contain the four strings: "there", "are", "4", and "parameters"

    Here is an example of how to access the command line arguments from the args parameter: example

    0 讨论(0)
  • 2020-12-13 02:18

    For passing in command line parameters. For example args[0] will give you the first command line parameter, if there is one.

    0 讨论(0)
提交回复
热议问题