开发者

Run external application with no .exe extension

I know how to run an external application in C# Syst开发者_高级运维em.Diagnostics.Process.Start(executableName); but what if the application I want to run has extension that is not recognizable by Windows as extension of an executable. In my case it is application.bin.


Key is to set the Process.StartInfo.UseShellExecute property to false prior to starting the process, e.g.:

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

This will start the process directly: instead of going through the "let's try to figure out the executable for the specified file extension" shell logic, the file will be considered to be executable itself.

Another syntax to achieve the same result might be:

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


And to follow on from @yelnic. Try using cmd.exe /C myapp, I found it quite useful when I want a little more out of 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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜