/*** 应用程序工具类 Austin Liu 刘恒辉 Project Manager and Software Designer E-Mail: lzhdim@163.com Blog: http://lzhdim.cnblogs.com Date: 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 sealed class AppUtil { /// <summary> /// 重启应用程序 /// </summary> public static void RestartApplication(string filePath) { Application.ExitThread(); // 启动应用程序 StartProcess(filePath, false); // 退出当前应用程序 Environment.Exit(0); } /// <summary> /// 启动应用程序 /// </summary> /// <param name="filePath">应用路径</param> public static void StartProcess(string filePath, bool isAdminStart) { //判断是否可执行文件 string path = FileSeparateUtil.GetPath(filePath); if (!File.Exists(path)) { return; } if (!(Path.GetExtension(path) == ".exe" || Path.GetExtension(path) == ".lnk")) { return; } //如果是快捷方式,找到应用主程序 if (Path.GetExtension(path) == ".lnk") { path = ShortCutUtil.GetTargetPathFromShortcut(path); if (!File.Exists(path)) { return; } //应用主程序,加上参数 filePath = ShortCutUtil.GetTargetPathFromShortcutWithParam(filePath) + " " + FileSeparateUtil.GetParams(filePath); } //这里判断是否需要以管理员身份运行 ProcessStartInfo processStartInfo = new ProcessStartInfo { UseShellExecute = false, // 不使用系统外壳程序启动 CreateNoWindow = true // 不显示窗口 }; if (isAdminStart) processStartInfo.Verb = "runas"; //以管理员身份运行 if (FileSeparateUtil.GetParams(filePath) == "") { //如果无参数,直接运行 processStartInfo.FileName = filePath; } else { //如果有参数,设置参数 processStartInfo.FileName = path; processStartInfo.Arguments = FileSeparateUtil.GetParams(filePath); } Process.Start(processStartInfo); } } }
![]() |
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |




浙公网安备 33010602011771号