简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM