Restart an application by itself

后端 未结 10 614
醉话见心
醉话见心 2020-12-13 02:46

I want to build my application with the function to restart itself. I found on codeproject

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments=\"/C          


        
相关标签:
10条回答
  • A lot of people are suggesting to use Application.Restart. In reality, this function rarely performs as expected. I have never had it shut down the application I am calling it from. I have always had to close the application through other methods such as closing the main form.

    You have two ways of handling this. You either have an external program that closes the calling process and starts a new one,

    or,

    you have the start of your new software kill other instances of same application if an argument is passed as restart.

            private void Application_Startup(object sender, StartupEventArgs e)
            {
                try
                {
                    if (e.Args.Length > 0)
                    {
                        foreach (string arg in e.Args)
                        {
                            if (arg == "-restart")
                            {
                                // WaitForConnection.exe
                                foreach (Process p in Process.GetProcesses())
                                {
                                    // In case we get Access Denied
                                    try
                                    {
                                        if (p.MainModule.FileName.ToLower().EndsWith("yourapp.exe"))
                                        {
                                            p.Kill();
                                            p.WaitForExit();
                                            break;
                                        }
                                    }
                                    catch
                                    { }
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
    
    0 讨论(0)
  • 2020-12-13 03:21

    Winforms has the Application.Restart() method, which does just that. If you're using WPF, you can simply add a reference to System.Windows.Forms and call it.

    0 讨论(0)
  • 2020-12-13 03:23

    For .Net application solution looks like this:

    System.Web.HttpRuntime.UnloadAppDomain()
    

    I used this to restart my web application after changing AppSettings in myconfig file.

    System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
    configuration.AppSettings.Settings["SiteMode"].Value = model.SiteMode.ToString();
    configuration.Save();
    
    0 讨论(0)
  • 2020-12-13 03:25

    My solution:

            private static bool _exiting;
        private static readonly object SynchObj = new object();
    
            public static void ApplicationRestart(params string[] commandLine)
        {
            lock (SynchObj)
            {
                if (Assembly.GetEntryAssembly() == null)
                {
                    throw new NotSupportedException("RestartNotSupported");
                }
    
                if (_exiting)
                {
                    return;
                }
    
                _exiting = true;
    
                if (Environment.OSVersion.Version.Major < 6)
                {
                    return;
                }
    
                bool cancelExit = true;
    
                try
                {
                    List<Form> openForms = Application.OpenForms.OfType<Form>().ToList();
    
                    for (int i = openForms.Count - 1; i >= 0; i--)
                    {
                        Form f = openForms[i];
    
                        if (f.InvokeRequired)
                        {
                            f.Invoke(new MethodInvoker(() =>
                            {
                                f.FormClosing += (sender, args) => cancelExit = args.Cancel;
                                f.Close();
                            }));
                        }
                        else
                        {
                            f.FormClosing += (sender, args) => cancelExit = args.Cancel;
                            f.Close();
                        }
    
                        if (cancelExit) break;
                    }
    
                    if (cancelExit) return;
    
                    Process.Start(new ProcessStartInfo
                    {
                        UseShellExecute = true,
                        WorkingDirectory = Environment.CurrentDirectory,
                        FileName = Application.ExecutablePath,
                        Arguments = commandLine.Length > 0 ? string.Join(" ", commandLine) : string.Empty
                    });
    
                    Application.Exit();
                }
                finally
                {
                    _exiting = false;
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-13 03:25

    This worked for me:

    Process.Start(Process.GetCurrentProcess().MainModule.FileName);
    Application.Current.Shutdown();
    

    Some of the other answers have neat things like waiting for a ping to give the initial application time to wind down, but if you just need something simple, this is nice.

    0 讨论(0)
  • 2020-12-13 03:26

    You have the initial application A, you want to restart. So, When you want to kill A, a little application B is started, B kill A, then B start A, and kill B.

    To start a process:

    Process.Start("A.exe");
    

    To kill a process, is something like this

    Process[] procs = Process.GetProcessesByName("B");
    
    foreach (Process proc in procs)
       proc.Kill();
    
    0 讨论(0)
提交回复
热议问题