Closing a running executable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

System.Diagnostics.Process.Start("c:\\windows\\system32\\notepad.exe","c:\\x.txt");

launches x.txt in notepad

What code will close this instance of notepad?
 
Hi!

System.Diagnostics.Process.Start("c:\\windows\\system32\\notepad.exe","c:\\x.txt");
launches x.txt in notepad
What code will close this instance of notepad?

//You need a reference to the process
Process p = new Process();
ProcessStartInfo psi =
new ProcessStartInfo(
"c:\\windows\\system32\\notepad.exe");
p.StartInfo = psi;

//Start notepad
p.Start();

//Sleep five seconds
System.Threading.Thread.Sleep(5000);

//Close notepad again
p.CloseMainWindow();

Cheers

Arne Janning
 
The following code opens a text file in notepad and then closes notepad ...
as desired.

System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo("c:\\windows\\system32\\notepad.exe","c:\\x.txt");
p.StartInfo = psi;
p.Start();
p.CloseMainWindow();


But, the following code opens a pdf file in the Acrobat reader but then does
not close the Acrobat reader.

System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo("c:\\program files\\adobe\\acrobat
6.0\\reader\\acrord32.exe",@"/t ""c:\exportfiles\d.pdf"" ""HP LaserJet 3300
Series PCL 6"" ""HP LaserJet 3300 Series PCL 6"" ""DOT4_001""");
p.StartInfo = psi;
p.Start();
p.CloseMainWindow();

How can I close this Acrobat reader programatically?
 
If you look through the help on the System.Diagnostics.Process class, you'll
note that there are "Close()" and "Kill" methods, as well as
"WaitForExit()", etc. so "p.Kill()" will (rudely) kill the running process.

CloseMainWindow() sends the window an exit message, which is the nice way of
doing it with a windows executable. However, it may or may not behave the
way you want it to.
 
Back
Top