Andrei Stanca
Andrei Stanca

Reputation: 908

how to execute shell command with arguments?

I`m trying to execute a shell command with arguments in C# , and "The system cannot find the file specified" is thrown.

I`ve tried:

p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\timesync\\NistClock.exe sync";

the path is correct 100% NistClock.exe gets executed when is run without the parameter "sync"

Upvotes: 0

Views: 6221

Answers (3)

David Morton
David Morton

Reputation: 16505

Use the Arguments property.

p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\timesync\\NistClock.exe";
p.StartInfo.Arguments = "sync";

By the way, be careful about using Directory.GetCurrentDirectory(). Note that this method can return something different if you're using any file dialogs throughout your application. It might be a better option to use something like Assembly.GetExecutingAssembly().Location instead, and parse the directory from there.

Upvotes: 3

juergen d
juergen d

Reputation: 204746

string path = Directory.GetCurrentDirectory() + "\\timesync\\NistClock.exe";                
string args = "sync";
ProcessStartInfo p = new ProcessStartInfo(path, args);
Process process = Process.Start(p);

Upvotes: 5

Adriano Repetti
Adriano Repetti

Reputation: 67070

You should change a little bit your code:

p.StartupInfo.FileName = Path.Combine(Directory.GetCurrentDirectory(), "timesync\\NistClock.exe");
p.StartupInfo.Arguments = "sync";

Upvotes: 6

Related Questions