How can I restart my WPF application using C#?
Asked
Active
Viewed 2.8k times
31
-
What do you mean by 'restart'? – Kirk Broadhurst Oct 09 '10 at 00:36
-
very simple close it and restart it automatically :D – kartal Oct 09 '10 at 00:37
4 Answers
31
I don't think there's a direct method in WPF like there is in WinForms. However, you could use methods from the Windowns.Form
namespace like this: (You might need to add a reference to the System.Windows.Form
assembly)
System.Windows.Forms.Application.Restart();
System.Windows.Application.Current.Shutdown();

keyboardP
- 68,824
- 13
- 156
- 205
-
Don't know what this is meant to achieve, in my case it reopens many instances of the same app without closing the first instances. – Denis G. Labrecque Jul 05 '22 at 18:11
16
The code
Process.Start(Application.ResourceAssembly.Location);
does not work for .NET Core 3.1 applications. Instead use
using System.Diagnostics;
using System.Windows;
...
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Application.Current.Shutdown();
This works also for .NET Framework applications.

Hans-Peter Kalb
- 201
- 2
- 4
-
Thanks, I am using 3.1 but `Application.Current.Shutdown()` wasn't available so I used `Application.Exit()` instead – hatsrumandcode Oct 05 '20 at 16:20
14
The following is the best solution I found, you don't need to add a reference to System.Windows.Forms, instead you need add the namespace System.Diagnostics
which you already has a reference to its assembly:
Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();

Mohammed A. Fadil
- 9,205
- 7
- 50
- 67
-
7Note that you don't want to use this method if your application is deployed with ClickOnce. The `ApplicationDeployment.IsNetworkDeployed` will be false when you restart. See http://bit.ly/RKoVBz for more info. If your application is not deployed with ClickOnce, this method works great. – blachniet Oct 21 '12 at 14:16
-
2@blachniet Also, you will start the *old* version after an update if using this method. This issue is also avoided when using the winforms method. – John Jun 13 '15 at 17:50
3
You can use the Windows API Code Pack's Restart and Recovery API. Just be aware that this is a new API, so it will only work on current operating systems (i.e.: Windows 7).

Toni
- 1,555
- 4
- 15
- 23

Reed Copsey
- 554,122
- 78
- 1,158
- 1,373