How to close .exe application on button click

Can anyone tell me how to close the .exe file on button click using C #. I have an idea how to start an .exe file on button click using C # like this:

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

      

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

+3


source to share


5 answers


I think you want to call CloseMainWindow()

on Process

. This is similar to clicking the close button of the window, so it may not close the application.

For example, if the user edited some text in the window but did not save it, the message “Do you want to save your changes?” Appears. dialog window.



If you really want to close the application, it doesn't matter, you can use Kill()

. This may result in data loss (edits to the file will not be saved), but this may not be a problem for you.

+4


source


You can use:

Process.Kill();

      



Or kill all instances of notepad you could do:

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

      

+1


source


Are you asking for Process.Close () ?

+1


source


You should write a click below your button something like this:

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

      

This will close your current window if its MDI child window

. Otherwise, it will close the application (if you only have one window open).

+1


source


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

      

0


source







All Articles