Пример #1
0
        void MoveFolderToOld(string oldPath, string newPath)
        {
            if (oldPath.IndexOf(ConstFile.AppExe) >= 0)
            {
                CommonUnitity.KillProcess(ConstFile.AppExe);
            }
            LogHelper.Debug(string.Format("MoveFolderToOld oldPath:{0},newPath:{1}", oldPath, newPath));
            if (File.Exists(oldPath + ".old"))
            {
                LogHelper.Debug("1");
                File.Delete(oldPath + ".old");
            }

            if (File.Exists(oldPath))
            {
                LogHelper.Debug("2");
                File.Move(oldPath, oldPath + ".old");
            }
            LogHelper.Debug("3");
            File.Move(newPath, oldPath);
            LogHelper.Debug("4");

            try
            {
                File.Delete(oldPath + ".old");
            }
            catch
            {
            }

            LogHelper.Debug("5");
        }
Пример #2
0
        public void Update()
        {
            if (!config.Enabled)
            {
                return;
            }
            if (ConstFile.AppPid != 0)
            {
                CommonUnitity.KillProcess(ConstFile.AppPid);
            }
            bool flag = CommonUnitity.isRunProcessName(ConstFile.AppExe);

            if (flag)
            {
                DialogResult dr = MessageBox.Show(dc, string.Format("进程{0}正在运行,请确定是否要关闭!", ConstFile.AppExe), "自动升级", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    CommonUnitity.KillProcess(ConstFile.AppExe);
                }
                else
                {
                    CommonUnitity.RestartApplication();
                    return;
                }
            }
            string urls = "";

            if (config.Type == 0)
            {
                using (var webClient = new HttpWebClient())
                {
                    try
                    {
                        urls = webClient.DownloadString(config.ServerUrl);
                    }
                    catch
                    {
                        try
                        {
                            urls = webClient.DownloadString(config.ServerUrl);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                        }
                    }
                }
            }

            else if (config.Type == 1)
            {
                urls = config.ServerUrl;
            }
            string[] strs = urls.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            if (strs.Length > 0)
            {
                RemoteConfig remoteConfig = null;
                int          count        = 2;
                int          retryCount   = 0;
                do
                {
                    foreach (var str in strs)
                    {
                        try
                        {
                            remoteConfig = ParseRemoteXml(str);
                            if (remoteConfig != null)
                            {
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(str + ":" + ex.Message);
                        }
                    }
                    retryCount++;
                } while (remoteConfig == null && retryCount < count);

                if (remoteConfig != null)
                {
                    string version  = remoteConfig.Version;
                    string mversion = remoteConfig.MinimumRequiredVersion;

                    LogHelper.Info(string.Format("version:{0},mversion:{1}", version, mversion));
                    LogHelper.Info(string.Format("comment:{0}", remoteConfig.Comment));
                    try
                    {
                        Version v = new Version(version);
                        config.Version = v.ToString();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                    }


                    Dictionary <string, RemoteFile> listRemotFile = remoteConfig.RemoteFiles;
                    List <DownloadFileInfo>         downloadList  = new List <DownloadFileInfo>();

                    foreach (LocalFile file in config.UpdateFileList)
                    {
                        if (listRemotFile.ContainsKey(file.Path))
                        {
                            try
                            {
                                RemoteFile rf = listRemotFile[file.Path];
                                Version    v1 = new Version(rf.LastVer);
                                Version    v2 = new Version(file.LastVer);
                                if (v1 > v2 || rf.Size != file.Size || rf.MD5 != file.MD5)
                                {
                                    downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size));
                                    file.LastVer = rf.LastVer;
                                    file.Size    = rf.Size;
                                    file.MD5     = rf.MD5;
                                    if (rf.NeedRestart)
                                    {
                                        bNeedRestart = true;
                                    }
                                    bDownload = true;
                                }

                                listRemotFile.Remove(file.Path);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error(ex);
                                //EventLog.WriteEntry("AutoUpdater Update", ex.StackTrace, EventLogEntryType.Error);
                            }
                        }
                    }

                    foreach (RemoteFile file in listRemotFile.Values)
                    {
                        downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size));
                        LocalFile lf = new LocalFile(file.Path, file.LastVer, file.Size, file.MD5);
                        config.UpdateFileList.Add(lf);
                        if (file.NeedRestart)
                        {
                            bNeedRestart = true;
                        }
                        bDownload = true;
                    }
                    downloadFileListTemp = new List <DownloadFileInfo>();
                    foreach (var downloadFileInfo in downloadList)
                    {
                        downloadFileListTemp.Add(downloadFileInfo);
                    }
                    if (bDownload)
                    {
                        dc.downloadFileList = downloadList;
                        //dc.IsCanCancel = isCanCancel;
                        dc.IsCanCancel = true;
                        if (this.OnShow != null)
                        {
                            this.OnShow();
                        }

                        if (DialogResult.OK == dc.ShowDialog())
                        {
                            StartDownload(downloadList);
                        }
                        else
                        {
                            CommonUnitity.RestartApplication();
                        }
                    }
                }
                else
                {
                    LogHelper.Error("remoteConfig is null");
                    CommonUnitity.ShowErrorAndRestartApplication(
                        string.Format("{0}更新不成功[无法获取升级清单文件,请检查网络是否正常],请单击退出程序", ConstFile.AppName), false);
                }
            }
            else
            {
                LogHelper.Error("url is null");
                CommonUnitity.ShowErrorAndRestartApplication(
                    string.Format("{0}更新不成功[无法连接到升级服务器,请检查网络是否正常],请单击退出程序", ConstFile.AppName), false);
            }
        }