C# 啟動、關閉 Process


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];
        }

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM