I'm new to the programming world.
I have a data extractor program which extracts data from MySQL into a word template file. I don't understand how the main program knows which argument to pass into the parameters. When running the exe, how does the program knows that "Haha, I need to pass a string in there, And it is precisely this string right here"
???.
EDIT: This program is supposed to be run when pressing a `Extraction Button' but how the exe program knows where is the string arg parameters? where does the exe get it from?
MAIN CODE
private static void Main(string[] args)
{
try
{
if (args[0] == "ALL" || args[0] == "*" || args[0] == "all")
{
PrintARs();
Console.Read();
}
else
{
CreateARDocument(args[0]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
}
}
There are a number of ways:
OK from your code heres some ideas
if your app was called app.exe
if you ran
app.exe 1 2 3 4 5
you would have args[] = { 1,2,3,4,5}
and can look through them
if you ran
app.exe /all c:\my stuff\a.txt
you would get args[] = { /all, c:\\my, stuff\\a.txt }
Your code looks at the first arg and says is it "ALL, * or all" .. so Awkward people writing "All" would lose out.. and then sends that arg in which ever way they wrote it, to the funciton.
如果您从命令行执行此操作,则它将
myApp.exe "arg0" "arg1" "arg2"
static void Main(string[] args) is the entrance of the application.
args is the parameters passed in to Main
args.Length is the how many parameters you passed in
set a break point in your main function
right click your project in solution explorer -> click property -> click debug -> put abcd in Command line arguments, run the application in debug mode, and check the value of args
Inside ExtractButton click event:
System.Diagnostics.Process.Start("PATH to your exe file", "Command Line Arguments");
Order of your command arguments is the same as they comes in args[] array.
Those are the Command-Line Arguments .
You may have noticed that you always have a Program.cs
file in your solution. That's where your Main()
method is located. In a windows form project, it doesn't take a string[] argument, though you can change that, and then it runs your application.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.