GettingStarted
GettingStarted

Reputation: 7625

Process.Start with arguments not working

I run this application in command line and get the desired results

 Helpdesk-02.exe /department it

but my C# code (below) appears to ignore the argument but launches the app without the command line switches

 ProcessStartInfo psi = new ProcessStartInfo();
 psi.FileName = @"Y:\Helpdesk-02.exe";
 psi.Arguments = @"/department it";
 psi.UseShellExecute = true;
 Process.Start(psi).WaitForExit();

Upvotes: 4

Views: 4578

Answers (2)

Berin Loritsch
Berin Loritsch

Reputation: 11463

The @ character is a special quoted string so it behaves differently than a standard string. Essentially what was happening is the process was being started with what would look like this from the command line:

>  Helpdesk-02.exe "/department it"

Or one argument. Removing the @ symbol forces C# to interpret the string as expected:

> Helpdesk-02.exe /department it

A subtle, but critical difference.

The @ operator was designed to make it easier to work with paths that have embedded spaces, backslashes, and other characters that have to be escaped in standard strings. Essentially, it does the character escaping for you. The two declarations are equivalent:

 string pathToExplorer = @"C:\Program Files\Internet Explorer\iexplore.exe";
 string escaped = "\"C:\\Program Files\\Internet Explorer\\iexplore.exe\"";

It's best to only use the @ operator when you are working with paths to files, and use the normal way when dealing with the parameters.

Upvotes: 5

Owen Pauling
Owen Pauling

Reputation: 11871

The documentation for ProcessStartInfo states:

spaces are interpreted as a separator between multiple arguments. A single argument that includes spaces must be surrounded by quotation marks, but those quotation marks are not carried through to the target application.

Upvotes: 0

Related Questions