我想用自己重新启动的函数来构build我的应用程序。 我在codeproject上find
ProcessStartInfo Info=new ProcessStartInfo(); Info.Arguments="/C choice /CY /N /DY /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
我使用类似的代码来重新启动应用程序时尝试的代码。 我发送一个定时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无法引号处理。
希望这可以帮助!
为什么不使用
Application.Restart();
??
更多关于重新启动
为什么不只是以下?
Process.Start(Application.ExecutablePath); Application.Exit();
如果你想确保应用程序不会运行两次,可以使用Environment.Exit(-1)
来瞬间杀死进程(不是真正的好方法),或者像启动第二个应用程序,它检查主应用程序的进程并在流程结束后立即重新启动。
你有最初的应用程序A,你想重新启动。 所以,当你想杀A时,一个小应用程序B被启动,B杀A,然后B启动A,杀B
开始一个过程:
Process.Start("A.exe");
杀死一个进程就是这样的
Process[] procs = Process.GetProcessesByName("B"); foreach (Process proc in procs) proc.Kill();
Winforms有Application.Restart()
方法,就是这样做的。 如果你使用的是WPF,你可以简单地添加一个对System.Windows.Forms
的引用并调用它。
很多人都建议使用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.Mainmodulee.FileName.ToLower().EndsWith("yourapp.exe")) { p.Kill(); p.WaitForExit(); break; } } catch { } } } } } } catch { } }
另一种比这些解决方案感觉更简洁的方法是运行一个包含特定延迟的批处理文件,以等待当前应用程序终止。 这具有防止两个应用程序实例同时打开的附加好处。
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();
我的解决方案
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; } } }
.Net应用程序解决方案如下所示:
System.Web.HttpRuntime.UnloadAppDomain()
在myconfig文件中更改AppSettings之后,我使用它来重新启动我的Web应用程序。
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~"); configuration.AppSettings.Settings["SiteMode"].Value = model.SiteMode.ToString(); configuration.Save();