此文记录的是一个应用程序重启的函数。

/***

    应用程序重启动

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    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 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();
            }
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.