-1

I have build a c# code which would basically take four arguments a1 a2 a3 a4. I am trying to make a batch file so that the user can enter his arguments and code gives the particular output. I am not sure how do i send these arguments to a batch file. i tried creating exe but it does not seem to work.

1 Answers1

5

To send arguments to a batch file, you do invoke it from the command line (or from another batch file) like this:

myfile.bat a1 a2 a3

Within the batch file, the arguments are represented by %1, %2, %3 (etc.), so within the batch file you would invoke your exe like this:

myapp.exe %1 %2 %3

That would pass the original arguments to the batch file, a1 a2 a3, along to the executable.

From within the executable you can access the arguments from your Main function

static void Main(string[] args)

The arguments, a1, a2, a3, would be in args[0], args[1] and args[2] respectively.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • So if i give %1 %2 %3 to make the batch file then from the command line try giving myfile.bat a1 a2 a3 will it work? – Rigorous implementation Jun 05 '12 at 22:36
  • 1
    Yeah, it should. And if your 'arguments' have spaces, you'll want to use double-quotes", i.e. "Argument 1 With Spaces", a2, a3 – JMC Jun 05 '12 at 22:41
  • sure thanks. i have got another doubt. in case i pass files is there a way where i can just specify the filename instead of whole path as arguments? i mean by making the path generic to all the systems? – Rigorous implementation Jun 05 '12 at 22:51
  • You can use relative paths (e.g. a path under where the .exe is installed... though users typically can't EDIT files stored there due to default permissions, see http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app), you can use a subfolder of the ApplicationData special folder (http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx), or you can configure a base path to your files in app.config. Then, use Path.Combine(myBasePath, a1) to get a full path to your file. – Eric J. Jun 05 '12 at 23:12
  • Hi i am getting an error. Index was out of boundary error though I am entering my args properly – Rigorous implementation Jun 06 '12 at 03:18