tags:

views:

44

answers:

3

how can I kill process or close whole application for my WPF application using c#?

+3  A: 

Are you using WinForms? Assuming you want to close it via code (and not from the window's close button), you could use Application.Exit. If you want to kill a process, then you can use the Process.Kill method.

Edit - Since you're using WPF, you can use the Application.Shutdown method.

keyboardP
using WPF in y application
salamonti
+1  A: 

You can Kill a process using Process.Kill, and close it nicely using Process.CloseMainWindow.

To find the process, use methods of the Process class such as Process.GetProcessById or Process.GetProcessesByName.

However, to kill another process (depending on the process), you may need elevated permissions. You'll need to start your executable with these in order for this to work, in that case.

You can use Environment.Exit to close your own process, or Environment.FailFast to kill it quickly.


Edit:

Since you're using WPF, you can also use Application.Shutdown to close your own application.

Reed Copsey
A: 

Use the Process class.

To avoid crashes, use an outer catch block - sometimes the System.Diagnostics classes cause operating system level exceptions that I usually want to ignore in the case of a small scale utility app.

Gabriel