Reputation: 5796
I'm developing a program, where the user can start any application from the program. The program will store the process id of the application started, so it can terminate it when the user wants to.
Process application = new Process();
application.StartInfo.FileName = txtApplicationToOpen.Text;
if (application.Start())
{
Debug.WriteLine("started");
lstCurrentlyOpenApplications.Items.Add(txtApplicationToOpen.Text);
_openApplications.Add(application);
}
The problem I'm facing :
the part in if(application.Start())
gets called, only if I'm opening say exe files, or an excel file ( though the PID returned by excel file doesn't kill the excel program ).
When I open a mp3, mp4, or a image file, it doesn't enter the if
statement
When trying to get the ID of the process, it returns the following error
System.InvalidOperationException: No process is associated with this object.
Upvotes: 5
Views: 2026
Reputation: 42494
In the description of what Process.Start
returns it says:
true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).
So if you start an EXE it is expected that calling Start
returns true (assuming the EXE exists). For the case where you don't give it an EXE it becomes a bit unpredictive what happens. And the core reason is because the shell is handling your Start
. If you re-run your tests with application.StartInfo.UseShellExecute = false;
you'll get widely different results. Turns out you can only start EXE (or COM files) when UseShellExecute is false.
The key part is that Start only returns true if an new process resource was started. In my testing I find that if it is first time I Start an .xlsx file a new Excel process is created (as a matter of fact it becomes a child process). That makes that Start()
returns true. For every next .xlsx file the existing process is re-used, so no new process is created, hence Start
returns false.
The same goes somewhat for when I start a .mp4 file. On my box that extension is still associated with the default Windows Movie player. That creates only a child process under an already running svchost service. That probably makes that false
is returned.
If I use an extension that is associated with my VLC player Start
returns true for each new file I offer it but by the looks of it the VLC player hands-off its work to the already running instance and then closes the newly started process.
process.Start
is returning the correct result. What it returns though depends on what gets started and how its registered handler choose to operate. Based on that you might or might not find a new process being started. It is worth noting that users have control over what is associated with a certain extension and as such what starting a file with a certain extension will do.
This is what my process tree looks like after running some experiments with several file types:
For those I got a process.Id that is still valid.
Upvotes: 0
Reputation: 17288
You have to use ProcessStartInfo.UseShellExecute
to be able to "execute" documents (and let windows find the program that is associated to the given file). See other answers there: ShellExecute vs. Process.Start.
Without that flag, only exe files can be started.
Upvotes: 1