0

I have a question. I want the project to restart when I press the cancel button in the MessageBox in C # I tried the following code Process.GetCurrentProcess (). Start (); but it didn't The full code is available below. There was a cancellation part, but I couldn't restart the project. Can you help me?

MessageBoxResult result = MessageBox.Show(message, title, MessageBoxButton.OKCancel, MessageBoxImage.Information);

                                            if (result == MessageBoxResult.OK)
                                            {
                                                Process.GetCurrentProcess().Kill();
                                            }
                                            else
                                            {
                                                Process.GetCurrentProcess().Refresh();
                                            }
  • `File -> New -> Project` works on my machine. – mxmissile May 03 '21 at 19:26
  • I'm not sure that you want to call Kill() on your current process. That tends to be violent to the process (IE, it doesn't allow a graceful shutdown). Instead, maybe what you want is to get the current full path to your exe (https://stackoverflow.com/questions/3991933/get-path-for-my-exe), then create a new Process object and start the process with your Exe path.... – B.O.B. May 03 '21 at 20:14
  • ....You can also look at passing along some kind of command line argument to let the new process know that it's because of a restart. Then in your current process, call something like Application.Current.Shutdown (https://learn.microsoft.com/en-us/dotnet/api/system.windows.application.shutdown?view=net-5.0) – B.O.B. May 03 '21 at 20:15

1 Answers1

0

You can use Application.Restart() for reference kindly refer https://learn.microsoft.com/en-us/dotnet/api/system.windows.application?view=net-5.0#properties to know more about Application object

Amit Bisht
  • 4,870
  • 14
  • 54
  • 83