Guy Dubrovski
Guy Dubrovski

Reputation: 1560

Running command line with c# not working

I have the following C# code that runs a command line:

ProcessStartInfo si = new ProcessStartInfo();
si.FileName = @"Lib\my_program.exe";

si.WindowStyle = ProcessWindowStyle.Hidden;
si.UseShellExecute = false;
si.CreateNoWindow = true;

si.Arguments = "my args";

Process p = new Process();
p.StartInfo = si;
p.Start();

It works perfect on one computer, but when running it on different computer - nothing happens.

When trying to run it via the command line at the problematic computer - it also works fine.

all the paths are correct and I run it with an admin permission.

What can go wrong? maybe some environment variables? or computer's security issues?

Upvotes: 0

Views: 258

Answers (2)

prabhakaran
prabhakaran

Reputation: 5274

Check here ProcessStartInfo.EnvironmentVariable["Path"] . If this doesn't have the "my_program.exe"'s parent folder. Then probably it is not knowing the location. Remember, here "Path" is case sensitive. Thanks go to my senior who cleared my doubt today on this one.

Upvotes: 0

Rob
Rob

Reputation: 1492

As it works when running from the command line, I would say that your application shortcut is running the program with a different working directory (so the relative path Lib\my_program.exe doesn't resolve to an existing program).

More generally, Process.Start() can throw various exceptions (probably FileNotFoundException in this case) so I suggest you wrap the code with a try/catch block and write the exception to Console.Error or display an error dialog.

Upvotes: 1

Related Questions