4

I have a C# project that takes in arguments that I'm trying to run from a bat file. For an application that doesn't take arguments I just put the following inside a file called run.bat

pathname\helloworld\bin\Debug\helloworld.exe 

What if my program takes in parameters, how do I adjust. When is the echo of used? any good tutorial on writing batch files? Thanks

Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
Luke Makk
  • 1,157
  • 3
  • 13
  • 13

4 Answers4

5
pathname\helloworld\bin\Debug\helloworld.exe "argument 1" "argument 2" 3

using System;
public class Demo {
    public static void Main(string[] args) {
        foreach(string arg in args)
            Console.WriteLine(arg);
    }
}
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
2

I would try

@rem turn off echo - atsign is line-level way how to do it
@echo off
@rem provided your app takes three params, this is how to pass them to exe file
pathname\helloworld\bin\Debug\helloworld.exe %1 %2 %3
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
0

String arguments tend to just follow the EXE after a space. So if you have two parameters "Bob", and "is a jerk" you could write this in the .bat:

helloworld.exe Bob "is a jerk"

Bob becomes the first parameter, since it has whitespace around it. But "is a jerk" is all one because of the quotation marks. So this would be two parameters.

Your tags mention C, but I'm unclear if you actually meant you're calling this from C, the completely seperate language; you seem to be just indicating you use a batch file.

Katana314
  • 8,429
  • 2
  • 28
  • 36
0

For your bat file, just add parameters after your exe path, like this:

pathname\helloworld\bin\debug\helloworld.exe param1 param2

Then, there's a method in your Program.cs file that looks like this:

[STAThread]
static void Main(string[] args)
{
  Application.Run(args.Length > 0 ? new Main(args[0]) : new Main());
}

Here you can tweak the parameters that are processed and send them to your startup form.

As for the echo, that's just like a print statement, anything you want to output to the console window...

ganders
  • 7,285
  • 17
  • 66
  • 114