views:

114

answers:

3

Hello,

i need to execute command line from .NET windows application. I tried with this code but i get 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);
+5  A: 

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);
Oded
+5  A: 

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.

Phil Lamb
A: 

Besides the fact that you don't need to start your process via cmd.exe (as already mentioned in another answer), I think you need to separate the command-line arguments from the name of the process to start; ie. try something similar to this:

var command   = @"C:\...\test.exe";
var arguments = @"D:\abc.pdf ...";

var processInfo = new ProcessStartInfo(command, arguments)
                  {
                      CreateNoWindow = true,
                      ...  // add other options as needed
                  };

var p = Process.Start(processInfo);

As you can see, the executable's name and the command-line arguments have moved into separate parameters of the ProcessStartInfo constructor. (You obviously need to put the proper strings back in there; I abbreviated them for clarity's sake.)

stakx