Skip to main content
 首页 » 编程设计

c#之执行二进制文件时出错

2024年02月06日12zdz8207

好的,你好。我有一些代码应该执行我的二进制文件并打印所有输出:

Process Program = new Process(); 
Program.StartInfo.FileName = "file.bin"; 
Program.StartInfo.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Build." + this.name; 
Program.StartInfo.RedirectStandardOutput = true; 
Program.StartInfo.UseShellExecute = false; 
Program.Start(); 
string output = Program.StandardOutput.ReadToEnd(); 
Program.WaitForExit(1000); 
Console.Out.WriteLine(output); 

但是当我运行它时我得到了这个错误:

在屏幕截图中,您可以看到文件名和完整路径。好的,我们进入控制台:

嘿!但是那个文件存在!我已经用相对路径试过了。还是行不通。

附言单声道,Ubuntu 14.04
附言当我删除 UseShellExecute = false 时,我的文件在 gedit 中打开。
P.P.P.S.文件 100% 存在:

var fi = new FileInfo(Path.Combine("Build." + this.name, "file.bin")); 
Console.Out.WriteLine(fi.Exists); //true 

请您参考如下方法:

来自 ProcessStartInfo.UseShellExecute 的文档

“当 UseShellExecute 为 false 时,WorkingDirectory 属性不用于查找可执行文件。相反,它仅由进程使用已启动且仅在新进程的上下文中有意义。当 UseShellExecute 为 false 时,FileName 属性必须是可执行文件的完全限定路径。"