0

I have this code to start a command line app:

        private void LaunchCommandLineApp(string latestStudents, string latestTopics)
    {
        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "ConsoleApplication2.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = 

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
            // Log error.
        }
    }

What is the correct syntax for passing latestStudents & latestTopics at the line startInfo.Arguments = as arguments? I've tried everything I can think of and some but I still don't get it!

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
Luves2spooge
  • 78
  • 1
  • 8
  • 2
    try just like command prompt:startInfo.Arguments ="latestStudents latestTopics"; – Mohammad Mirmostafa Apr 26 '16 at 10:02
  • Separate your arguments by space. Here is a link, the first one actually, after Googling https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx – monstertjie_za Apr 26 '16 at 10:03
  • Do you have the code of _ConsoleApplication2.exe_? If yes then it is trivial to see how the app expects the arguments passed. If not then you should at least have the documentation that explains how to put the arguments on the command line (if any way to do this has been programmed). Without these infos and without your failed attempts any answer is a guess. – Steve Apr 26 '16 at 10:07
  • 1
    If either of those inputs may contain spaces or quote characters, you may want to look at [this question](http://stackoverflow.com/q/5510343/15498) that deals with the complexity involved in *escaping* the inputs adequately. – Damien_The_Unbeliever Apr 26 '16 at 10:07

4 Answers4

1

Arguments is a string, which the documentation unhelpfully says is interpreted entirely by the target application. It does say now .NET applications will interpret it, so it really depends on what process you're launching.

The only way to know how to make that arguments string do the right thing for the process you're trying to pass it to is to find out how that process handles its arguments (try running it from the command line if you need to experiment). Mostly you can expect it to expect them to be separated with spaces. It's possible that you can just do something like (assuming C# 6):

$"{latestStudents} {latestTopics}"

But that might not work, depending on what's inside those variables. They may need to be quoted, especially if they contain spaces themselves.

There really is no definitive answer I can give you.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
  • This didn't work either. The console app takes the 2 arguments and displays them as strings. Essentially, `Console.WriteLine(arg[0]);` & `Console.WriteLine(arg[1]);` but I'm getting index outside of the bounds of the array error. – Luves2spooge Apr 26 '16 at 11:03
  • I got it to work. It was actually a round error in my console app, not how the strings were being passed. Thank you! – Luves2spooge Apr 26 '16 at 11:30
1

It depends on the program that is interpreting the arguments, but generally if you separate the arguments with spaces then they will be presented to the program as an array of strings.

For example, if you specify the argument string as:

startInfo.Arguments = "one two   three  \"fo ur\" \"\\\"fi ve\"\\\""

Then if the program is a C# console application, the Main(string[] args) method will receive an args array as follows:

args[0] == "one"
args[1] == "two"
args[2] == "three"
args[3] == "fo ur"
args[4] == "\"fi ve\""

Note that consecutive spaces such as those between "two" and "three" in my example are ignored.

Also note that the quotes around "fo ur" cause that to be passed as a single argument.

Finally, note that if you want to pass quotes as part of a parameter, you have to escape them with a backslash. In C#, of course, you have to escape the backslash and the quotes, so instead of "\"fi ve\"" in my example, I have to write the even-more unwieldy \"\\\"fi ve\"\\\""

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • "Also note that the quotes around "fo ur" are preserved" Since when? I was under the impression that `args[3]` would be `"fo ur"`. – yaakov Apr 26 '16 at 10:07
0

ProcessStartInfo arguments are passed as a string, similar to if you were running ConsoleApplication2.exe via a command line.

For example if you had a command window open in Windows and were to run something like ConsoleApplication2.exe /help, that would be passing "/help" in as a command argument.

So for your case (and it depends how ConsoleApplication2.exe is coded), you'll want to do something like:

startInfo.Arguments = latestStudents + " " latestTopics;

...assuming ConsoleApplication2.exe accepts those two parameters in that order.

jkriddle
  • 708
  • 1
  • 6
  • 15
  • This didn't work either. The console app takes the 2 arguments and displays them as strings. Essentially, Console.WriteLine(arg[0]); & Console.WriteLine(arg[1]); but I'm getting index outside of the bounds of the array error – Luves2spooge Apr 26 '16 at 11:03
  • You'll need to debug ConsoleApplication2.exe and see if it is getting any input. `for (var i = 0; i < args.Length; i++) { Console.WriteLine("[" + i + "] = " + args[i]); }` – jkriddle Apr 26 '16 at 11:59
0

Examples:

        ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        // Start with one argument.
        // Output of ArgsEcho:
        //  [0]=/a            
        startInfo.Arguments = "/a";
        Process.Start(startInfo);

        // Start with multiple arguments separated by spaces.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b
        //  [2] = c:\temp
        startInfo.Arguments = "/a /b c:\\temp";
        Process.Start(startInfo);

        // An argument with spaces inside quotes is interpreted as multiple arguments.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = literal string arg
        startInfo.Arguments = "/a \"literal string arg\"";
        Process.Start(startInfo);

        // An argument inside double quotes is interpreted as if the quote weren't there,
        // that is, as separate arguments. Equivalent verbatim string is @"/a /b:""string with quotes"""
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b:string
        //  [2] = in
        //  [3] = double
        //  [4] = quotes
        startInfo.Arguments = "/a /b:\"\"string in double quotes\"\"";
        Process.Start(startInfo);

        // Triple-escape quotation marks to include the character in the final argument received
        // by the target process. Equivalent verbatim string: @"/a /b:""""""quoted string""""""";
        //  [0] = /a
        //  [1] = /b:"quoted string"
        startInfo.Arguments = "/a /b:\"\"\"quoted string\"\"\"";
        Process.Start(startInfo);

It is amazing what you can find while Googling for something...

This is the link I used as source for this above code samples.

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73