aron
aron

Reputation: 1904

Executing a command line from a Windows application

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

Answers (2)

Phil Lamb
Phil Lamb

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

Oded
Oded

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

Related Questions