Does Process.StartInfo.UseShellExecute = true work with Process.WaitForExit()?
It can, but not in the scenario you have.
UseShellExecute
(which is set to true
by default in the older desktop .NET versions, and set to false
by default for .NET Core) controls whether the Process
class will directly create the process, or will delegate that to the Windows shell.
In the latter case, one of the main reasons it's used that way is to take advantage of the Windows file associations to automatically select the program to run for the file you've specified. But when this happens, the actual process that started is not available to the Process
class.
Only if you explicitly specify the executable file that should run will you be able to actually monitor the process after starting it (and of course, if that's what you're doing, you don't actually need to set UseShellExecute
to true
).
In your specific example, if you already know what program it is you want to use to open the .mp4 file, you can change your process start information so that you provide that program's name for the ProcessStartInfo.FileName
property, and then the .mp4 file you want to open in the Arguments
property. Of course, this assumes that the program you want to use accepts filename arguments when it's run, but this is commonly the case for media player type programs.
For example, if you wanted to use Windows Media Player to play the .mp4 file, you could do something like this:
p.StartInfo = new ProcessStartInfo
{
FileName = @"C:\Program Files (x86)\Windows Media Player\wmplayer.exe",
Arguments = @"something.mp4",
UseShellExecute = false, // Not needed on .NET Core, since that's already the default
};
Note that you may actually need to specify the full path of the file, not just the file name, depending on what working directory the process winds up started in and how the program you are trying to start handles that. But as an example, I have found that Windows Media Player ignores the working directory set when starting the process, and will fail to find and start playing the file you specify, unless you provide the full path to the file.
Other media players may or may not behave similarly.