How to restart program in C#
By FoxLearn 3/4/2025 2:04:12 AM 658
To start the same program instance as current process and close the current process at the same time. Depending on application types, the implementation might be a little different, but the basic idea is same.
For example, c# restart application
private void RestartProgram() { // Get the current application's executable path var exePath = Assembly.GetExecutingAssembly().Location; // var exePath = Application.ExecutablePath; // for WinForms // Start a new instance of the application Process.Start(exePath); // For Windows Forms app Application.Exit(); // For all Windows application but typically for Console app. //Environment.Exit(0); }
System.Reflection.Assembly.GetEntryAssembly().Location
is used to get the path of the currently executing program.
Process.Start(exePath)
starts a new instance of the application.
Environment.Exit(0)
ends the current process, effectively "restarting" it by launching a fresh instance.
In WinForms applications, you can use Application.Restart()
to restart the program.
This approach works well for restarting desktop applications, but may not be suitable for ASP.NET applications or services.
- Primitive types in C#
- How to set permissions for a directory in C#
- How to Convert Int to Byte Array in C#
- How to Convert string list to int list in C#
- How to convert timestamp to date in C#
- How to Get all files in a folder in C#
- How to use Channel as an async queue in C#
- Case sensitivity in JSON deserialization