C# Main 类中的“string[] args”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/552796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "string[] args" in Main class for?
提问by Ali
In C# the Main class has string[] args parameter.
在 C# 中,Main 类具有 string[] args 参数。
What is that for and where does it get used?
它的用途是什么,在哪里使用?
采纳答案by Daniel Richardson
From the C# programming guide on MSDN:
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
回答by andynormancx
For passing in command line parameters. For example args[0]
will give you the first command line parameter, if there is one.
用于传递命令行参数。例如args[0]
会给你第一个命令行参数,如果有的话。
回答by Frederick The Fool
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
}
}
回答by Adam Ralph
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".
这是传递给程序的命令行开关数组。例如,如果您使用命令“ myapp.exe -c -d
”启动程序,那么string[] args[]
将包含字符串“-c”和“-d”。
回答by MahlerFive
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
回答by Drew Noakes
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...
}
}
回答by Jamshaid Kamran
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.
除了其他答案。您应该注意到这些参数可以为您提供拖放到.exe
文件上的文件路径。即,如果您将任何文件拖放到您的.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. e.g
这将打印放置在.exe
文件上的文件的路径。例如
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. See:
因此,循环遍历args
数组将为您提供所有已选择并拖放到.exe
控制台应用程序文件上的文件的路径。看:
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
应用程序上。
回答by Ben Rauzi
It's an array of the parameters/arguments (hence args) that you send to the program. For example ping 172.16.0.1 -t -4
它是您发送到程序的参数/参数(因此为 args)的数组。例如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
// 包含参数的字符串数组。