private void ShowErrorAndExitApp()
 {
     MessageBox.Show(Constant.Notnetwork, Constant.Messagetitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
     CommonUnitity.ExitApp(ExitCode.UpdateError);
     ExitCode = ExitCode.UpdateError;
 }
        private void ProcDownload(object o)
        {
            string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername);

            if (!Directory.Exists(tempFolderPath))
            {
                Directory.CreateDirectory(tempFolderPath);
            }

            _evtPerDonwload = new ManualResetEvent(false);

            foreach (DownloadFileInfo file in _downloadFileList)
            {
                _total += file.Size;
            }

            CommonUnitity.OnLog(this, new EventArgs <string>("tempFolderPath=" + tempFolderPath));
            try
            {
                while (!_evtDownload.WaitOne(0, false))
                {
                    if (_downloadFileList.Count == 0)
                    {
                        break;
                    }

                    DownloadFileInfo file = _downloadFileList[0];

                    CommonUnitity.OnLog(this, new EventArgs <string>(String.Format("Start Download: url={0}, fullname={1}", file.DownloadUrl, file.FileFullName)));

                    ShowCurrentDownloadFileName(file.FileName);

                    //Download
                    _clientDownload = new WebClient();

                    //Added the function to support proxy
                    _clientDownload.Proxy             = WebProxy.GetDefaultProxy();
                    _clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    _clientDownload.Credentials       = CredentialCache.DefaultCredentials;
                    //End added

                    _clientDownload.DownloadProgressChanged += (sender, e) =>
                    {
                        try
                        {
                            SetProcessBar(e.ProgressPercentage, (int)((_nDownloadedTotal + e.BytesReceived) * 100 / _total));
                        }
                        catch
                        {
                            CommonUnitity.OnLog(this, new EventArgs <string>("progress changed failed." + e.ProgressPercentage + "/" + e.TotalBytesToReceive));
                            //log the error message,you can use the application's log code
                        }
                    };

                    _clientDownload.DownloadFileCompleted += (sender, e) =>
                    {
                        try
                        {
                            DealWithDownloadErrors();
                            DownloadFileInfo dfile = e.UserState as DownloadFileInfo;
                            _nDownloadedTotal += dfile.Size;
                            SetProcessBar(0, (int)(_nDownloadedTotal * 100 / _total));
                            _evtPerDonwload.Set();

                            CommonUnitity.OnLog(this, new EventArgs <string>("downloadFinished:" + dfile.FileFullName));
                        }
                        catch (Exception ex)
                        {
                            CommonUnitity.OnLog(this, new EventArgs <string>(ex.ToString()));
                            //log the error message,you can use the application's log code
                        }
                    };

                    _evtPerDonwload.Reset();

                    //Download the folder file
                    string tempFolderPath1 = DownloadFileInfo.GetFolderUrl(file);
                    if (!string.IsNullOrEmpty(tempFolderPath1))
                    {
                        tempFolderPath  = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername);
                        tempFolderPath += tempFolderPath1;
                    }
                    else
                    {
                        tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername);
                    }

                    _clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl),
                                                      Path.Combine(tempFolderPath, file.FileName), file);

                    //Wait for the download complete
                    _evtPerDonwload.WaitOne();

                    _clientDownload.Dispose();
                    _clientDownload = null;

                    //Remove the downloaded files
                    _downloadFileList.Remove(file);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine(ex.Source);
                CommonUnitity.OnLog(this, new EventArgs <string>(ex.StackTrace));
                ShowErrorAndExitApp();
                //throw;
            }

            //When the files have not downloaded,return.
            if (_downloadFileList.Count > 0)
            {
                return;
            }

            //Test network and deal with errors if there have
            DealWithDownloadErrors();

            //Debug.WriteLine("All Downloaded");
            foreach (DownloadFileInfo file in _allFileList)
            {
                string tempUrlPath = DownloadFileInfo.GetFolderUrl(file);
                string oldPath     = string.Empty;
                string newPath     = string.Empty;
                try
                {
                    if (!string.IsNullOrEmpty(tempUrlPath))
                    {
                        oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName);
                        newPath = Path.Combine(CommonUnitity.SystemBinUrl + Constant.Tempfoldername + tempUrlPath, file.FileName);
                    }
                    else
                    {
                        oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName);
                        newPath = Path.Combine(CommonUnitity.SystemBinUrl + Constant.Tempfoldername, file.FileName);
                    }

//                    //just deal with the problem which the files EndsWith xml can not download
//                    FileInfo f = new FileInfo(newPath);
//                    if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.EndsWith(".xml"))
//                    {
//                        Console.WriteLine("download failed: {0}", f.FullName);
//                        ShowErrorAndExitApp();
//                    }

                    //Added for dealing with the config file download errors
                    string newfilepath = string.Empty;
                    if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(Constant.Configfilekey))
                    {
                        if (File.Exists(newPath))
                        {
                            if (newPath.EndsWith("_"))
                            {
                                newfilepath = newPath;
                                newPath     = newPath.Substring(0, newPath.Length - 1);
                                oldPath     = oldPath.Substring(0, oldPath.Length - 1);
                            }
                            File.Move(newfilepath, newPath);
                        }
                    }
                    //End added

                    if (File.Exists(oldPath))
                    {
                        MoveFolderToOld(oldPath, newPath);
                    }
                    else
                    {
                        //Edit for config_ file
                        if (!string.IsNullOrEmpty(tempUrlPath))
                        {
                            if (!Directory.Exists(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1)))
                            {
                                Directory.CreateDirectory(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1));

                                MoveFolderToOld(oldPath, newPath);
                            }
                            else
                            {
                                MoveFolderToOld(oldPath, newPath);
                            }
                        }
                        else
                        {
                            MoveFolderToOld(oldPath, newPath);
                        }
                    }
                }
                catch (Exception exp)
                {
                    CommonUnitity.OnLog(this, new EventArgs <string>(exp.ToString()));
                    //log the error message,you can use the application's log code
                }
            }

            //After dealed with all files, clear the data
            _allFileList.Clear();

            if (_downloadFileList.Count == 0)
            {
                Exit(true);
            }
            else
            {
                Exit(false);
            }

            _evtDownload.Set();
        }
Exemple #3
0
        public ExitCode Update()
        {
            if (!_config.Enabled)
            {
                CommonUnitity.OnLog(this, new EventArgs <string>("local config file not allowed update."));
                return(ExitCode.Default);
            }

            try
            {
                _remoteConfig =
                    SerializerHelper.Deserialize(typeof(RemoteConfig), RequestUrl(_config.ServerUrl)) as RemoteConfig;
            }
            catch (Exception ex)
            {
                CommonUnitity.OnLog(this, new EventArgs <string>(ex.Message));
                return(ExitCode.Default);
            }
            if (_remoteConfig == null)
            {
                CommonUnitity.OnLog(this, new EventArgs <string>("load remote config file failed."));
                return(ExitCode.Default);
            }

            if (!_remoteConfig.Enable)
            {
                CommonUnitity.OnLog(this, new EventArgs <string>("remote server not allowed update."));
                return(ExitCode.Default);
            }

            if (!CommonUnitity.ForceUpdate)
            {
                CommonUnitity.ForceUpdate = _remoteConfig.ForceUpdate;
            }

            List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>();

            string dir = AppDomain.CurrentDomain.BaseDirectory;

            foreach (RemoteFile remoteFile in _remoteConfig.UpdateFileList)
            {
                string localFile = Path.Combine(dir, remoteFile.LocalPath);
                if (remoteFile.Md5.Equals(HashHelper.ComputeSHA1(localFile), StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                CommonUnitity.OnLog(this, new EventArgs <string>("FileToBeDownload:" + remoteFile));
                downloadList.Add(new DownloadFileInfo(remoteFile.Url, remoteFile.LocalPath, remoteFile.LastVer, remoteFile.Size));

                if (remoteFile.NeedRestart)
                {
                    _bNeedRestart = true;
                }
            }

            _downloadFileListTemp = downloadList;

            ExitCode exitCode = 0;

            if (_downloadFileListTemp != null && !_downloadFileListTemp.IsEmpty())
            {
                if (CommonUnitity.ForceUpdate)
                {
                    exitCode = StartDownload(downloadList);
                }
                else
                {
                    DownloadConfirm dc = new DownloadConfirm(downloadList);
                    if (this.OnShow != null)
                    {
                        this.OnShow();
                    }

                    if (DialogResult.OK == dc.ShowDialog())
                    {
                        exitCode = StartDownload(downloadList);
                    }
                    else
                    {
                        exitCode = ExitCode.SkipUpdate;
                        CommonUnitity.OnLog(this, new EventArgs <string>("Update skipped."));
                        Application.Exit();
                    }
                }
            }
            return(exitCode);
        }