Reputation: 1904
I need to execute a command line from a .NET windows application.
I tried with this code, but I got an error:
'C:\Documents' is not recognized as an internal or external command, operable program or batch file.
var command ="\"C:\\Documents and Settings\\Administrator\\My Documents\\test.exe\" \"D:\\abc.pdf\" \"C:\\Documents and Settings\\Administrator\\My Documents\\def.pdf\"";
var processInfo = new ProcessStartInfo("cmd","/c " + command)
{
UseShellExecute = false,
RedirectStandardError = true,
CreateNoWindow = true
};
var p = Process.Start(processInfo);
Upvotes: 1
Views: 2841
Reputation: 1437
Try using the overloaded version of Process.Start and pass the parameters in the second argument:
var command = @"C:\Documents and Settings\Administrator\My Documents\test.exe";
var parameters = @"""D:\abc.pdf"" ""C:\Documents and Settings\Administrator\My Documents\def.pdf""";
var p = Process.Start(command, parameters);
This is assuming that you're trying to call test.exe with the PDF files as parameters.
Upvotes: 6
Reputation: 498904
I don't think you need to shell out to cmd
. Just call the exe directly:
var command ="\"C:\\Documents and Settings\\Administrator\\My Documents\\test.exe\" \"D:\\abc.pdf\" \"C:\\Documents and Settings\\Administrator\\My Documents\\def.pdf\"";
var processInfo = new ProcessStartInfo(command)
{
UseShellExecute = false,
RedirectStandardError = true,
CreateNoWindow = true
};
var p = Process.Start(processInfo);
Upvotes: 6