-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.

Mister JJ
  • 292
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [Passing command-line arguments in C#](https://stackoverflow.com/questions/653563/passing-command-line-arguments-in-c-sharp) – Code Name Jack Jul 01 '18 at 17:52
  • Possible duplicate of [How to get command line parameters and put them into variables?](https://stackoverflow.com/questions/24661047/how-to-get-command-line-parameters-and-put-them-into-variables) – Lance U. Matthews Jul 01 '18 at 18:00

2 Answers2

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.

Code Name Jack
  • 2,856
  • 24
  • 40
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.

Tyrrrz
  • 2,492
  • 1
  • 19
  • 29