What exactly does System.Diagnostics.Process UseShellExecute do?

前端 未结 4 670
予麋鹿
予麋鹿 2021-02-02 10:09

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 batc

4条回答
  •  礼貌的吻别
    2021-02-02 10:57

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

    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.

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

    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.

提交回复
热议问题