Process.Start()不启动.exe文件(手动运行时有效) [英] Process.Start() not starting the .exe file (works when run manually)

查看:376
本文介绍了Process.Start()不启动.exe文件(手动运行时有效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.exe文件,创建文件后需要运行该文件.该文件已成功创建,之后,我使用以下代码运行.exe文件:

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = pathToMyExe;
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;                        
Process proc = Process.Start(processInfo);

我也尝试了一个简单的Process.Start(pathToMyExe);,但是.exe文件未运行.当我在 Windows资源管理器上手动尝试pathToMyExe时,程序将正确运行.但是不能通过程序.我看到的是光标转向等待几秒钟,然后又恢复正常.因此,也没有抛出异常.什么阻止了文件?

解决方案

您没有设置工作目录路径,并且与通过资源管理器启动应用程序不同,它不会自动设置为可执行文件的位置.

只需执行以下操作:

processInfo.WorkingDirectory = Path.GetDirectoryName(pathToMyExe);

(假设输入文件,DLL等在该目录中)

I have an .exe file that needs to be run after I create a file. The file is successfully created and I am using the following code to run the .exe file after that:

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = pathToMyExe;
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;                        
Process proc = Process.Start(processInfo);

I also tried with a simple Process.Start(pathToMyExe); but the .exe file is not run. When I try pathToMyExe manually on my Windows Explorer the program is correctly run. But not via the program. What I see is the cursor turning to waiting for a few seconds and then back to normal. So there are no Exceptions thrown either. What is blocking the file?

解决方案

You are not setting the working directory path, and unlike when starting the application through Explorer, it isn't set automatically to the location of the executable.

Just do something like this:

processInfo.WorkingDirectory = Path.GetDirectoryName(pathToMyExe);

(assuming the input files, DLLs etc. are in that directory)

这篇关于Process.Start()不启动.exe文件(手动运行时有效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆