-2

I'm trying to create a C# console app to do some proccess. I want to open my demo.exe and send some parameters from a .BAT file to that cosole.

I know the .bat should be something like:

demo.exe -a cclock -cc 1306 -mc 1750

But, I don't have any idea to make my .exe to get the parameters I'm sending.

2

2 Answers 2

3

This is where the arguments to Main method helps.

In an standard C# program entry method is like,

static int Main(string[] args)

Here args[] is the array of arguments passed to your executable via command line. So in your example,

demo.exe -a cclock -cc 1306 -mc 1750

args is a string array containing following,

{"-a", "cclock", "-cc", "1306", "-mc", "1750"}

You can retrieve these value in this manner,

args[0]  = "-a"
args[1] = "cclock"
args[2]= "-cc" ...... and so on

You can use these value for the rest of your code.

Remember that whatever values you pass are broken into separate string values on each occurrence of white-space. Also whatever value you pass will be taken as string. So you have to do your own validation and parsing.

Sign up to request clarification or add additional context in comments.

Comments

1

Your application's Main method (typically in Program.cs) can accept a parameter string[] args, which you can access to get the command line parameters used to launch your app. Alternatively, you can also use Environment.GetCommandLineArgs() anywhere in the application to do the same thing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.