Here is the simple program:
class Program {
static void Main(string[] args) {
Process pr = new Process();
ProcessStartInfo prs = new ProcessStartInfo();
prs.FileName = @"notepad.exe";
pr.StartInfo = prs;
pr.EnableRaisingEvents = true;
pr.Start();
Thread.Sleep(2000);
pr.Close(); //how do I change this to close notepad
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
}
How do I change this so Notepad closes?
According to MSDN, Process.Close() simply:
Frees all the resources that are associated with this component.
Take a look at Process.Kill() and Process.CloseMainWindow()
For this application, I would use:
pr.CloseMainWindow();
pr.Close();
Try with Process.CloseMainWindow() method instead of.
Closes a process that has a user interface by sending a close message to its main window.
Try like this;
Process pr = new Process();
ProcessStartInfo prs = new ProcessStartInfo();
prs.FileName = @"notepad.exe";
pr.StartInfo = prs;
pr.EnableRaisingEvents = true;
pr.Start();
Thread.Sleep(2000);
pr.CloseMainWindow();
pr.Close();
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With