Reputation: 5322
I opened an explorer instance via
System.Diagnostics.Process.Start("explorer.exe", @<path>);
however, I also want to close this exact process at a given time.
I tried this so far:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.Start("explorer.exe", @<path>);
However, that doesn't seem to work at all. This is what I'm getting when I try it that way:
"Member '' cannot be accessed with an instance reference; qualify it with a type name instead"
I am fairly sure it's something pretty simple, but I can't get around it...
Any help?
Upvotes: 0
Views: 2896
Reputation: 13248
The issue that you had is that that the method Process.Start() is a static
method and does not need an instance of the Process
class to be called.
As others have said, Start()
returns a Process
object that you can then work with and can call Kill()
later on at some point.
Upvotes: 1
Reputation: 12546
System.Diagnostics.Process.Start
returns Process
var proc = System.Diagnostics.Process.Start("explorer.exe", @<path>);
proc.Kill();
Upvotes: 4
Reputation: 157116
Create the Process
using ProcessStartInfo
:
ProcessStartInfo psi = new ProcessStartInfo("explorer.exe", @"C:\some.file");
Process p = Process.Start(psi);
///
p.Kill();
Call Kill
on the process when you want to.
Upvotes: 3