此文记录的是一个应用程序重启的函数。
/***应用程序重启动Austin Liu 刘恒辉Project Manager and Software DesignerE-Mail: lzhdim@163.comBlog: http://lzhdim.cnblogs.comDate: 2024-01-15 15:18:00使用方法:AppUtil.RestartApplication(Application.ExecutablePath);说明:1、用于应用程序关闭后再启动操作,具体在使用时能够修改该代码;2、比如应用的更新操作,将应用的EXE执行文件复制过来覆盖,然后重启;***/namespace Lzhdim.LPF.Utility
{using System;using System.IO;using System.Diagnostics;using System.Windows.Forms;/// <summary>/// 应用工具类/// </summary>public class AppUtil{/// <summary>/// 重启应用程序/// </summary>public static void RestartApplication(string filePath){Application.ExitThread();// 启动应用程序StartProcess(filePath);// 退出当前应用程序Environment.Exit(0);}/// <summary>/// 启动应用程序/// </summary>/// <param name="filename"></param>private static void StartProcess(string filename){if (!File.Exists(filename)){return;}if (!(Path.GetExtension(filename) == ".exe")){return;}int index = filename.IndexOf('/');if (index <= 0){Process.Start(filename);}else{int length = filename.Length - index;string fileName = filename.Substring(0, index - 1);string arguments = filename.Substring(index, length);Process process = new Process{StartInfo = new ProcessStartInfo(fileName, arguments)};process.StartInfo.UseShellExecute = false;process.Start();}}}
}