What exactly does System.Diagnostics.Process UseShellExecute do?

前端 未结 4 669
予麋鹿
予麋鹿 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:37

    The use of 'UseShellExecute' IIRC, is to allow explorer (the main shell) to execute the process and not the .NET runtime....unless somebody corrects me that I'm wrong...

    0 讨论(0)
  • 2021-02-02 10:56

    did you specify WorkingDirectory correctly? You can see actual output of your command by adding >c:\log.txt 2>c:\err.txt run it wil this addition and check those files

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-02 11:04

    From the Documentation:

    Setting this property to false enables you to redirect input, output, and error streams.

    Note: UseShellExecute must be false if the UserName property is not a null reference (Nothing in Visual Basic) or an empty string, or an InvalidOperationException will be thrown when the Process.Start(ProcessStartInfo) method is called. When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, with the Process component. When UseShellExecute is false, you can start only executables with the Process component.

    Note: UseShellExecute must be true if you set the ErrorDialog property to true. The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.

    When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and has meaning only within the context of the new process.

    0 讨论(0)
提交回复
热议问题