C# 代码运行exe

 

 //检查更新
        private bool UpdateCheck()
        {
            
                Logger.Debug("开始更新");

                //if (DebugHelper.IsDebug)
                //{
                //    return true;
                //}
                bool updateCheck = Converter.Convert<bool>(ConfigurationManager.AppSettings["Update.Check"]);
                //如果配置文件中关闭了更新功能,则不执行任何更新的动作
                if (!updateCheck)
                {
                    return true;
                }
                string baseDir = System.AppDomain.CurrentDomain.BaseDirectory;
                string url = ConfigurationManager.AppSettings["Update.Server"];

                // 用ProcessStartInfo启动一个exe程序
                string args = string.Format("{0} {1} {2}", ToBase64String(url), ToBase64String(baseDir), ToBase64String(baseDir + "Starter.exe"));

                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = baseDir + "Updater.exe";
                start.Arguments = args;
                start.UseShellExecute = false;
                start.RedirectStandardOutput = true;
                Logger.Debug("启动Update.exe:");
                //启动调用
                using (Process process = Process.Start(start))
                {
                    //接收控制台输出的消息
                    process.OutputDataReceived += (s1, e1) =>
                    {
                        if (e1 != null && e1.Data != null && e1.Data.Equals("Update"))
                        {
                            Process[] p = System.Diagnostics.Process.GetProcessesByName("Starter");
                            p[0].Kill();
                        }
                    };
                    process.BeginOutputReadLine();
                    //等待退出
                    process.WaitForExit();
                }


                #region 开始执行更新程序 wupan
                //Process process = new Process();
                //process.StartInfo.FileName = baseDir + "Updater.exe";
                //process.StartInfo.Arguments = args;
                //process.StartInfo.UseShellExecute = false;
                //process.StartInfo.RedirectStandardOutput = true;
                //process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
                //{
                //    // Prepend line numbers to each line of the output.
                //    if (e != null && e.Data != null && e.Data.Equals("Update"))
                //    {

                //        Process[] p = System.Diagnostics.Process.GetProcessesByName("Starter");
                //        p[0].Kill();
                //    }
                //});

                启动
                //process.Start();

                开始进行异步读取工作
                //process.BeginOutputReadLine();
                无限期等待关联进程退出
                //process.WaitForExit();
                释放所有关联资源
                //process.Close();


                #endregion
                return true;
            
        }

 

     /// <summary>
        /// 检查更新程序
        /// </summary>
        /// <returns></returns>
        private void CheckUpdateExe()
        {
            
                string address = ConfigurationManager.AppSettings["Update.Server"] + "/Updater.exe";

                string ServerPath = AppDomain.CurrentDomain.BaseDirectory + "TemporaryFiles" + "/Updater.exe";
                string LocalPath = AppDomain.CurrentDomain.BaseDirectory + "Updater.exe";

                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(ServerPath));
                WebClient webClient = new WebClient();
                webClient.DownloadFile(address, ServerPath);
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(LocalPath));
                if (MD5(LocalPath) != MD5(ServerPath))
                {
                    //不是文件夹即复制文件,true表示可以覆盖同名文件
                    File.Copy(ServerPath, LocalPath, true);
                }

                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "TemporaryFiles");
                di.Delete(true);//删除文件夹
           
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值