如何确定是否将ProcessStartInfo.UseShellExecute属性设置为true或false?

时间:2019-07-11 11:47:21

标签: c# .net windows process

我正在从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);

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

1 个答案:

答案 0 :(得分:1)

您可以在.NET源代码中看到:

  • UseShellExecute = true =>

内部函数 StartWithShellExecuteEx =>调用ShellExecuteEx

  • UseShellExecute = false =>

内部函数 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

相关问题