Night Walker
Night Walker

Reputation: 21280

Run application from C# code with multiple arguments

I have following list of arguments that I want to run some application with.

 "C:\Release one" "My Manager" 321

I see that I should put them to ProcessStartInfo Arguments property.

But how do I write them correctly because they have spaces in the strings, "My Manager"?

Upvotes: 2

Views: 517

Answers (6)

ígor
ígor

Reputation: 1164

I'm using this methot to get CorrectaArgumentvalue

 public static string GetArgumentValue(string arg)
    {
        if (arg.Contains(" "))
            return string.Format("\"{0}\"", arg);
        return arg;
  }

Example:

     process.StartInfo.Arguments = string.Format("{0} {1} {2}", GetArgumentValue(@"C:\Release one"), GetArgumentValue("My Manager"), 321);

Upvotes: 0

Tirumudi
Tirumudi

Reputation: 443

Use this:

 System.Diagnostics.Process.Start("something.exe","arg_1"+"  "+"arg_2"+"  "+"arg_3"+"  ");

Upvotes: 1

Habib
Habib

Reputation: 223392

Try:

process.StartInfo.Arguments = "\"My Manager\" 321";

Upvotes: 5

codeteq
codeteq

Reputation: 1532

Escape the " and the \

Example:

Process p = new Process();
p.StartInfo.FileName = "C:\\Release one";
p.StartInfo.Arguments = "\"My Manager\" 321";
p.Start();

Upvotes: 0

Kevin
Kevin

Reputation: 704

Try...

"\"C:\\Release one\""
"\"My Manager\""
321

I've not tried this in your particular situation, but it is the standard way to include double quote as a part of the string.

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

You need to place them as you show there, in quotes.

So the code might look like

Process.Start("myexe.exe","\"My stuff\" "+myarg);

Upvotes: 0

Related Questions