欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace AutoUpdate.Helper
{
    public class ProcessHelper
    {
        private static ProcessHelper instance;
        public static ProcessHelper Instance
        {
            get
            {
                if (instance == null) instance = new ProcessHelper();
                return ProcessHelper.instance;
            }
        }

        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="processName">进程名</param>
        public void KillProcessByName(string processName)
        {
            try
            {
                Process[] myproc = Process.GetProcesses();
                foreach (Process item in myproc)
                {
                    if (item.ProcessName == processName)
                    {
                        item.CloseMainWindow();
                        item.Kill();
                    }
                }
            }
            catch { return; }
        }

        /// <summary>
        /// 启动进程
        /// </summary>
        /// <param name="processName">进程名,完全限定名</param>
        public void StartProcessByName(string processName)
        {
            System.Diagnostics.Process.Start(processName);
            //Process process = new Process();//声明一个进程类对象
            //process.StartInfo.FileName = processName;//Application.StartupPath + "\\AutoUpdate.exe";
            //process.Start();
        }

    }
}
           //初始路径 + 自动更新程序名称 + 主程序名称 
            string param = fullPath + "," 
                + "AutoUpdate\\AutoUpdate.exe," //自动更新程序
                + "Debug\\AutoUpdateApp.exe,"//主程序
                + "Debug";//下载包

            //AutoUpdate
            Process p = new Process();//声明一个进程类对象
            p.StartInfo.FileName = fullPath + "AutoUpdate\\AutoUpdate.exe";
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.Arguments = param;//第一个参数为需要下载的压缩文件,第二个为需要启动的程序
            p.Start();

注:被启动的应用使用方式如下所示:
 /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常  
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常  
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            //MessageBox.Show("参数==" + args[0]);
            InitVariable(args);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        private static void InitVariable(string[] args)
        {
            //VariableHelper.Instance.UpdateExeDir = ConfigurationManager.AppSettings["UpdateExeDir"];
            string[] param = args[0].Split(',');
            VarHelper.Instance.InitPath = param[0];
            VarHelper.Instance.AutoUpdateExe = param[1];
            VarHelper.Instance.MainExe = param[2];
            VarHelper.Instance.DownDir = param[3];
        }

  

  

posted on 2021-01-29 10:24  sunwugang  阅读(1944)  评论(0编辑  收藏  举报