运行没有.exe扩展名的外部应用程序

问题描述 投票:16回答:2

我知道如何在C#System.Diagnostics.Process.Start(executableName);中运行外部应用程序,但如果我想运行的应用程序具有Windows无法识别为可执行文件扩展名的扩展。在我的情况下,它是application.bin

c# .net process
2个回答
30
投票

关键是在开始这个过程之前将Process.StartInfo.UseShellExecute属性设置为false,例如:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"c:\tmp\test.bin";
p.StartInfo.UseShellExecute  = false;
p.Start();

这将直接启动该过程:该文件将被视为可执行文件,而不是通过“让我们尝试找出指定文件扩展名的可执行文件”shell逻辑。

实现相同结果的另一种语法可能是:

var processStartInfo = new ProcessStartInfo
{
    FileName = @"c:\tmp\test.bin",
    UseShellExecute = false
};
Process.Start(processStartInfo);

3
投票

并从@yelnic继续。尝试使用cmd.exe /C myapp,我发现它非常有用,当我想要更多的Process.Start()

using (Process process = Process.Start("cmd.exe") 
{
   // `cmd` variable can contain your executable without an `exe` extension
   process.Arguments = String.Format("/C \"{0} {1}\"", cmd, String.Join(" ", args));
   process.UseShellExecute  = false;
   process.RedirectStandardOutput = true;
   process.Start();
   process.WaitForExit();
   output = process.StandardOutput.ReadToEnd();
}
© www.soinside.com 2019 - 2024. All rights reserved.