Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close .exe application on button click

Tags:

c#

process

Can any one tell me how can I close .exe file on button click using c#. I've got an idea of how to run .exe file on button click using c# as follows:

string str = @"C:\windows\system32\notepad.exe";
process.StartInfo.FileName = str;
process.Start();

But can anyone tell me how to close .exe application on button click in c#?

like image 565
user1304965 Avatar asked Mar 31 '12 11:03

user1304965


2 Answers

I think you want to call CloseMainWindow() on the Process. This is analogous to clicking on the close button of the window, so it may not actually close the application.

For example, if the user edited some text in the window, but didn't save it, this will show the “Do you want to save your changes?” dialog.

If you want to really close the application, no matter what, you can use Kill(). This may cause loss of data (the edits to the file won't be saved), but that may not be a problem for you.

like image 169
svick Avatar answered Sep 29 '22 18:09

svick


You should write under your button click something like this:

private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }

This will close your current window, if its MDI child window. Else it will close the application (assuming you got only one window open).

like image 24
Melvin Avatar answered Sep 29 '22 18:09

Melvin