0

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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Sossenbinder
  • 4,852
  • 5
  • 35
  • 78
  • check if process exists then terminate it: http://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name, the `at a given time` is up to you how you implement it! – Ric Aug 05 '15 at 08:51
  • If you mean stopping the process, Simply you use proc.Kill – Sirwan Afifi Aug 05 '15 at 08:52
  • Yes, but I can't even get the proc.Start to work. I get this error message: "Member '' cannot be accessed with an instance reference; qualify it with a type name instead" – Sossenbinder Aug 05 '15 at 08:54
  • The error clearly indicates that you shouldn't be creating a reference of Process for calling the start method. Call the start method with Type (System.Diagnostic) which will return the Process and then you can do operations like proc.Kill() – Ali Baig Aug 05 '15 at 08:59

3 Answers3

4

System.Diagnostics.Process.Start returns Process

var proc = System.Diagnostics.Process.Start("explorer.exe", @<path>); 
proc.Kill();
Eser
  • 12,346
  • 1
  • 22
  • 32
3

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

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.

Ric
  • 12,855
  • 3
  • 30
  • 36