简体   繁体   English

Main 类中的“string[] args”是什么?

[英]What is "string[] args" in Main class for?

In C# the Main class has string[] args parameter.在 C# 中,Main 类具有 string[] args 参数。

What is that for and where does it get used?它的用途是什么,在哪里使用?

From the C# programming guide on MSDN :来自MSDN 上C# 编程指南

The parameter of the Main method is a String array that represents the command-line arguments Main 方法的参数是一个 String 数组,表示命令行参数

So, if I had a program (MyApp.exe) like this:所以,如果我有一个这样的程序(MyApp.exe):

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". Main 方法将传递一个包含三个字符串的数组:“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

Further to everyone else's answer, you should note that the parameters are optional in C# if your application does not use command line arguments.除了其他人的回答之外,您应该注意,如果您的应用程序不使用命令行参数,则参数在 C# 中是可选的。

This code is perfectly valid:这段代码是完全有效的:

internal static Program
{
    private static void Main()
    {
        // Get on with it, without any arguments...
    }
}

For passing in command line parameters.用于传递命令行参数。 For example args[0] will give you the first command line parameter, if there is one.例如args[0]会给你第一个命令行参数,如果有的话。

Besides the other answers.除了其他答案。 You should notice these args can give you the file path that was dragged and dropped on the .exe file.您应该注意到这些参数可以为您提供拖放到.exe文件上的文件路径。 ie 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.即,如果您将任何文件拖放到.exe文件上,那么应用程序将被启动并且arg[0]将包含拖放到其上的文件路径。

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

this will print the path of the file dropped on the .exe file.这将打印放置在.exe文件上的文件的路径。 eg例如

C:\\Users\\ABCXYZ\\source\\repos\\ConsoleTest\\ConsoleTest\\bin\\Debug\\ConsoleTest.pdb 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.因此,遍历args数组将为您提供所有已选择并拖放到控制台应用程序的.exe文件上的文件的路径。 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.上面的代码示例将打印所有拖放到其上的文件名,请参阅我将 5 个文件拖到我的ConsoleTest.exe应用程序上。

5 文件被放置在 ConsoleTest.exe 文件中。 And here is the output that I get after that:这是我在那之后得到的输出: 输出

The args parameter stores all command line arguments which are given by the user when you run the program. args 参数存储用户在运行程序时给出的所有命令行参数。

If you run your program from the console like this:如果你像这样从控制台运行你的程序:

program.exe there are 4 parameters program.exe有4个参数

Your args parameter will contain the four strings: "there", "are", "4", and "parameters"您的 args 参数将包含四个字符串:“there”、“are”、“4”和“parameters”

Here is an example of how to access the command line arguments from the args parameter: example以下是如何从 args 参数访问命令行参数的示例example

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.如果您用 C# 编写一个这样的应用程序,数组args用作上述参数的集合。

This how you process them:这是您处理它们的方式:

static void Main(string[] args) {
    foreach (string arg in args) {
       //Do something with each argument
    }
}

This is an array of the command line switches pass to the program.这是传递给程序的命令行开关数组。 Eg if you start the program with the command " myapp.exe -c -d " then string[] args[] will contain the strings "-c" and "-d".例如,如果您使用命令“ myapp.exe -c -d ”启动程序,那么string[] args[]将包含字符串“-c”和“-d”。

It's an array of the parameters/arguments (hence args) that you send to the program.它是您发送到程序的参数/参数(因此为 args)的数组。 For example ping 172.16.0.1 -t -4例如ping 172.16.0.1 -t -4

These arguments are passed to the program as an array of strings.这些参数作为字符串数组传递给程序。

string[] args // Array of Strings containing arguments. string[] args // 包含参数的字符串数组。

That is for if you were going to run your application from the command line. 如果您要从命令行运行应用程序,那就是这样。 These parameters will be accessible in the args array. 这些参数可以在args数组中访问。 Go to http://www.c-sharpcorner.com/UploadFile/mahesh/CmdLineArgs03212006232449PM/CmdLineArgs.aspx for more details. 有关详细信息, 请访问http://www.c-sharpcorner.com/UploadFile/mahesh/CmdLineArgs03212006232449PM/CmdLineArgs.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM