自行重启应用程序

问题描述 投票:0回答:11

我想构建具有自动重启功能的应用程序。我在codeproject上找到了

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
               Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info); 
Application.Exit();

这根本不起作用... 另一个问题是,如何重新开始呢? 也许还有启动应用程序的参数。

编辑:

http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=31454&av=58703
c# .net windows batch-file application-restart
11个回答
48
投票

我使用与您重新启动应用程序时尝试的代码类似的代码。我发送一个定时 cmd 命令来为我重新启动应用程序,如下所示:

ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Application.Exit(); 

命令发送到操作系统,ping 会暂停脚本 2-3 秒,此时应用程序已从

Application.Exit()
退出,然后 ping 后的下一个命令会再次启动它。

注意:

\"
在路径周围加上引号,以防路径中有空格,如果没有引号,cmd 将无法处理。

希望这有帮助!


31
投票

为什么不使用

Application.Restart();

??

更多关于重新启动


9
投票

为什么不只是以下内容?

Process.Start(Application.ExecutablePath); 
Application.Exit();

如果您想确保应用程序不会运行两次,请使用

Environment.Exit(-1)
立即终止进程(不是很好的方法),或者启动第二个应用程序,它检查主应用程序的进程并启动它一旦该过程消失,就再次进行。


7
投票

您有初始应用程序A,您想要重新启动。 所以,当你想杀死A时,启动一个小应用程序B,B杀死A,然后B启动A,然后杀死B。

启动流程:

Process.Start("A.exe");

杀死一个进程,是这样的

Process[] procs = Process.GetProcessesByName("B");

foreach (Process proc in procs)
   proc.Kill();

5
投票

很多人建议使用Application.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
            {
            }
        }

2
投票

Winforms 有

Application.Restart()
方法,它就是这样做的。如果您使用的是 WPF,则只需添加对
System.Windows.Forms
的引用并调用它即可。


2
投票

另一种比这些解决方案更干净的方法是运行一个批处理文件,其中包含等待当前应用程序终止的特定延迟。这具有防止两个应用程序实例同时打开的额外好处。

Windows 批处理文件示例(“restart.bat”):

sleep 5
start "" "C:\Dev\MyApplication.exe"

在应用程序中,添加以下代码:

// Launch the restart batch file
Process.Start(@"C:\Dev\restart.bat");

// Close the current application (for WPF case)
Application.Current.MainWindow.Close();

// Close the current application (for WinForms case)
Application.Exit();

1
投票

我的解决方案:

        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;
            }
        }
    }

1
投票

这对我有用:

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

其他一些答案有一些巧妙的东西,比如等待

ping
给初始申请时间来结束,但如果你只需要一些简单的东西,这很好。


0
投票

对于 .Net 应用程序解决方案如下所示:

System.Web.HttpRuntime.UnloadAppDomain()

在更改 myconfig 文件中的 AppSettings 后,我用它来重新启动我的 Web 应用程序。

System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
configuration.AppSettings.Settings["SiteMode"].Value = model.SiteMode.ToString();
configuration.Save();

0
投票

如果应用程序作为 Windows 服务运行,则可以使用 powershell 轻松重新启动它:

var psi = new ProcessStartInfo(
    "pwsh",
    "-Command \"Start-Sleep -Seconds 3; Restart-Service -Name 'TheServiceName';\""
);
Process.Start(psi);
Environment.Exit(0);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.