user1304965
user1304965

Reputation: 51

how to close .exe application on button click

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#?

Upvotes: 5

Views: 10398

Answers (5)

Melvin
Melvin

Reputation: 21

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).

Upvotes: 2

Darren
Darren

Reputation: 70718

You could use:

Process.Kill();

Or to kill all instances of notepad you could do:

foreach (var process in Process.GetProcessesByName("notepad")) {
   process.Kill();
}

Upvotes: 1

svick
svick

Reputation: 244757

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.

Upvotes: 5

Amritpal Singh
Amritpal Singh

Reputation: 1785

    processes = Process.GetProcessesByName(procName);
    foreach (Process proc in processes)
    {
        if(proc.MainWindowTitle.equals(myTitle))
        {           
        proc.CloseMainWindow();
        proc.WaitForExit(); or use tempProc.Close();
        }
}

Upvotes: 0

Tigran
Tigran

Reputation: 62248

Do you asking for Process.Close() ?

Upvotes: 1

Related Questions