【问题标题】:How to decide whether or not to set ProcessStartInfo.UseShellExecute Property to true or false?如何决定是否将 ProcessStartInfo.UseShellExecute 属性设置为 true 或 false?
【发布时间】:2019-11-21 03:06:35
【问题描述】:

我正在从我的 C# windows 窗体应用程序调用/启动 .exe 应用程序和 .bat 批处理文件(我也在传递参数)。如何确定是否需要将 ProcessStartInfo.UseShellExecute 属性设置为 true 或 false?什么情况下真比假好,反之亦然?

我正在使用 ProcessStartInfo 类来设置所有进程信息。然后,我使用 System.Diagnostics.Process.Start(ProcessStartInfo) 方法根据我的 ProcessStartInfo 启动进程。


using System.Diagnostics;

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "[something]";
processInfo.Arguments = "[parameter1] [parameter2] [parameter3]";
Process.Start(processInfo);

我希望这些流程能够启动并正常运行。如果我需要添加更多信息/澄清某些事情,请告诉我,这是我的第一个问题。

【问题讨论】:

  • @Castorix 我还是不明白,有什么区别?为什么我应该选择使用 shellexecute 而不是不使用它
  • 我添加了一个包含更多细节的答案...

标签: c# .net windows process


【解决方案1】:

你可以在.NET源码中看到:

  • UseShellExecute = true =>

内部函数StartWithShellExecuteEx => 调用ShellExecuteEx

  • UseShellExecute = 假 =>

内部函数StartWithCreateProcess => 调用CreateProcessWithLogonWCreateProcessW

当您想要启动与扩展关联的可执行文件时,通常使用 true。 喜欢:

            using (Process p = new Process())
            {
                string sImage = @"e:\test.jpg";
                p.StartInfo.FileName = sImage;
                p.StartInfo.Verb = "open";
                p.StartInfo.UseShellExecute = true;
                p.Start();
            }

如果我设置为 false,它将失败,因为它会尝试使用 "e:\test.jpg" 作为命令行调用 CreateProcessW

【讨论】:

  • 谢谢,这对理解文档更有帮助
猜你喜欢
  • 2021-09-28
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
相关资源
最近更新 更多