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

5
  • c# and c are different languages. Please be more careful when tagging. Commented Aug 6, 2013 at 13:55
  • 1
    "pathname\helloworld\bin\Debug\helloworld.exe" "first param" "second param". Commented Aug 6, 2013 at 13:56
  • Try to use stackoverflow.com/questions/3268022/process-start-arguments Commented Aug 6, 2013 at 13:58
  • @IvayloStrandjev I guess it is more of a bat tag than anything else :) Commented Aug 6, 2013 at 13:59
  • can you post your code for the main method? since it will need to accept params to work, like string[] args is default on console apps Commented Aug 6, 2013 at 14:01

4 Answers 4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

1 Comment

I don't know why, but I kept scrolling up and down the page looking for someone named Bob...
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.