Ejemplo n.º 1
0
        private RemoteConfig ParseRemoteXml(string xml)
        {
            XmlDocument document = new XmlDocument();

            document.Load(xml);

            RemoteConfig remoteConfig = new RemoteConfig(document);

            return(remoteConfig);
        }
Ejemplo n.º 2
0
        public int IsUpdate()
        {
            int isUpdate = 0;

            if (config.Enabled)
            {
                string urls = "";
                LogHelper.Debug(string.Format("ServerUrl:{0}", config.ServerUrl));
                if (config.Type == 0)
                {
                    using (var webClient = new HttpWebClient())
                    {
                        try
                        {
                            urls = webClient.DownloadString(config.ServerUrl);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                        }
                    }
                    LogHelper.Debug(string.Format("AutoUpdaterUrl:{0}", urls));
                }
                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;

                    foreach (var str in strs)
                    {
                        try
                        {
                            remoteConfig = ParseRemoteXml(str);
                            break;
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                            //EventLog.WriteEntry("AutoUpdater isUpdate", ex.StackTrace, EventLogEntryType.Error);
                        }
                    }



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

                        try
                        {
                            Version mv = new Version(mversion);
                            Version vc = new Version(config.Version);

                            if (vc < mv)
                            {
                                isUpdate = 3;
                                return(isUpdate);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                            //EventLog.WriteEntry("AutoUpdater ", ex.StackTrace, EventLogEntryType.Error);
                        }


                        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;

                                        if (rf.NeedRestart)
                                        {
                                            bNeedRestart = true;
                                        }
                                        isUpdate = 2;
                                        break;
                                    }
                                    listRemotFile.Remove(file.Path);
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.Error(ex);
                                    //EventLog.WriteEntry("AutoUpdater IsUpdate", ex.StackTrace, EventLogEntryType.Error);
                                }
                            }
                        }

                        if (listRemotFile.Count > 0)
                        {
                            isUpdate = 2;
                        }
                    }
                    else
                    {
                        LogHelper.Error("AutoUpdater IsUpdate no autoUpdaterUrl:" + config.ServerUrl);
                        //EventLog.WriteEntry("AutoUpdater IsUpdate", "no autoUpdaterUrl:" + config.ServerUrl, EventLogEntryType.Error);
                    }
                }
                else
                {
                    LogHelper.Error("AutoUpdater IsUpdate no autoUpdaterUrl:" + config.ServerUrl);
                    //EventLog.WriteEntry("AutoUpdater IsUpdate", "no autoUpdaterUrl:" + config.ServerUrl, EventLogEntryType.Error);
                }
            }

            return(isUpdate);
        }
Ejemplo n.º 3
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);
            }
        }