ispiro
ispiro

Reputation: 27633

Trouble starting process through C#. Perhaps because of whitespace

I have (copied from the debugger after they were set, to make sure that's what is actually passed to the command):

process.StartInfo.FileName = "C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bin\\signtool.exe"

process.StartInfo.Arguments = " sign /f \"C:\\Users\\...\\myPfx.pfx\" /p \"myPassword\" \"C:\\Users\\...\\Desktop\\app.exe\""

but when I Start() it it doesn't sign the app. (When I copy signtool to that folder and do it manually - it works.) So, for debugging, I tried:

System.Diagnostics.Process.Start(@"cmd.exe", @" /k " + "\"" + process.StartInfo.FileName + "\"" + " " + "\"" + process.StartInfo.Arguments + "\"");

But I get:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

So how do I get the signing to work (or at least the cmd, so I'll be able to see what exactly is the problem)?

EDIT: Thanks everyone. The problem was as answered below - missing quotes. And though I did actually try that before posting the question (-adding quotes to everything) - it didn't work then. It turns out I was adding some white space between the quotes and the actual parameters. So it seems that that causes an error.

Upvotes: 1

Views: 1053

Answers (3)

Yves Rochon
Yves Rochon

Reputation: 1562

Please try this, create yourself a ProcessStartInfo object then set the Process's StartInfo to your new object:

ProcessStartInfo startInfo = new ProcessStartInfo();

string filename = "\"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bin\\signtool.exe\""
string arguments = " sign /f \"C:\\Users\\...\\myPfx.pfx\" /p \"myPassword\" \"C:\\Users\\...\\Desktop\\app.exe\""
startInfo.Arguments = filename + arguments;
startInfo.FileName = "cmd.exe";
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

startInfo.WorkingDirectory = "set your working directory here";
Process p = new Process();

p.StartInfo = startInfo;
p.Start();

string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.
//Instrumentation.Log(LogTypes.ProgramOutput, output);
//Instrumentation.Log(LogTypes.StandardError, error);

p.WaitForExit();

if (p.ExitCode == 0) 
{        
    // log success;
}
else
{
    // log failure;
}

Upvotes: 1

user1830285
user1830285

Reputation: 598

You may need to set the WorkingDirectory property to be the desktop (where you app.exe sits), and just pass "app.exe" (without the path) to the arguments property.

Upvotes: 0

Bali C
Bali C

Reputation: 31231

Your filename needs to be quoted

process.StartInfo.FileName = "\"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bin\\signtool.exe\""

EDIT

Try using this instead, which works for me with dummy files

string filename = "\"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bin\\signtool.exe\""
string arguments = "sign /f \"C:\\Users\\...\\myPfx.pfx\" /p \"myPassword\" \"C:\\Users\\...\\Desktop\\app.exe\""
Process.Start("cmd.exe /k " + filename + " " + arguments)

Upvotes: 3

Related Questions