究竟是什么的System.Diagnostics.Process UseShellExecute吗? [英] What exactly does System.Diagnostics.Process UseShellExecute do?

查看:356
本文介绍了究竟是什么的System.Diagnostics.Process UseShellExecute吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行(除其他事项外)一个MSBuild任务XCOPY通话。我所发现的是,这个调用XCOPY正确执行,当我运行一个批处理文件,我的MSBuild任务,而无法执行或产生任何输出,可以让我的想法是什么时同一批文件从另一个叫做要去。C#应用程序与的System.Diagnostics.Process

I have an MSBuild task that executes (among other things) a call to xcopy. What I have found is that this call to xcopy executes correctly when I run my MSBuild task from a batch file, and fails to execute or produce any output that would allow me any idea what is going on when that same batch file is called from another C# application with a System.Diagnostics.Process.

两个进程都或多或少相同的结构展开:

Both processes are launched with more or less the same structure:

waitProc.StartInfo.Arguments = "/C [executable]";
waitProc.StartInfo.FileName = "cmd.exe";
waitProc.StartInfo.UseShellExecute = false;



此外,通过改变从假UseShellExecute为true xcopy命令,我可以让这个成功的两者都使用的情况下,但该命令未能在第三个用例运行。第三个用例是我们的自动化构建系统这是一个窗口服务直接调用的MSBuild。在我们的构建机器上失败的情况下,复制命令无限期挂起这是,我相信,因为的System.Diagnostics.Process尝试显示一个窗口,服务没有与之关联的Windows桌面会话,因此他们不能显示窗口。

Furthermore by changing the "UseShellExecute" from false to true on the xcopy command I can make this succeed in both use cases, however the command fails to run in a third use case. The third use case being our automated build system which is a windows service calling msbuild directly. In the case of the failure on our build machine the copy command hangs indefinitely which is, I believe, because the System.Diagnostics.Process tries to display a window, and services do not have a Windows desktop session associated with them, so they cannot display windows.

我已经用CreateNoWindow财产试过,我已经尝试设置WindowStyle到ProcessWindowStyle.Hidden,但是这并没有改变在构建机上的行为。

I have tried using the "CreateNoWindow" property, and I've tried setting the "WindowStyle" to "ProcessWindowStyle.Hidden," but that does not change the behavior on the build machine.

这一切说,我真正想知道的是完全相同的UseShellExecute属性的作用,因为它似乎做了一大堆更多比MSDN文档建议。

All of this said, what I really want to know is what exactly the UseShellExecute property does, because it seems to do a whole lot more than the MSDN documentation suggests.

感谢。

推荐答案

<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx> ProcessStartInfo.UseShellExecute 告诉使用Windows shell执行指定的应用程序的过程

ProcessStartInfo.UseShellExecute tells the Process to use the Windows Shell to execute the specified application.

如果没有这一套,你只能直接执行EXE文件。通过设置此,您可以在Windows Shell中使用,这使得事情如指定.doc文件,并具有关联的程序打开该文件。

Without this set, you can only execute an EXE file directly. By setting this, you allow the Windows Shell to be used, which allows things such as specifying a .doc file and having the associated program open the file.

然而,使用Windows外壳程序需要一个有效的桌面背景,这就是为什么你的第三个用例失败。

However, using the Windows Shell requires a valid desktop context, which is why your third use case fails.

在一般情况下,使用的cmd.exe ,除非您使用的Windows外壳程序是有问题的。您可能希望只写代码,直接处理你的批量操作 - 即:使用来自种方法中的 System.IO命名空间做你的复制。这将避免此问题完全。

In general, using cmd.exe is problematic unless you're using the Windows Shell. You may want to just write the code to handle your "batch" operation directly - ie: use the methods from types in the System.IO namespace to do your copying. This would avoid this issue entirely.

这篇关于究竟是什么的System.Diagnostics.Process UseShellExecute吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆