Example #1
0
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;

            if (ex != null)
            {
                MessageBox.Show("发现异常:" + ex.Message);
            }
            AutoUpdateCoeus.WriteLogFileAndEmail(e);
        }
Example #2
0
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var ex = e.Exception as Exception;

            if (ex != null)
            {
                MessageBox.Show("发现异常:" + ex.Message);
            }
            AutoUpdateCoeus.WriteLogFileAndEmail(e);
        }
Example #3
0
        /// <summary>
        /// 开始更新
        /// </summary>
        private void BeginUpdate()
        {
            string tmpDir        = appPath + "\\" + AutoUpdateCoeus.TempUpgradeDirectory;
            bool   upgradeResult = false;

            try
            {
                if (!Directory.Exists(tmpDir))
                {
                    Directory.CreateDirectory(tmpDir);
                }
                _hasDownloadSize   = 0;
                _downloadTotalSize = 0;
                ShowInfo("获取升级包大小...");
                downLoadFileNameList.Clear();

                FTPFileInfo serverAllFileInfo          = new FTPFileInfo();
                FTPFileInfo serverDirectoryAndFileInfo = GetServerDirectoryAndFileInfo(serverAllFileInfo);
                if (serverDirectoryAndFileInfo == null)
                {
                    MessageBox.Show("升级服务器上不存在文件:UpgradeBaseFileList.xml,升级失败。");
                    //等待两秒钟之后关闭当前窗口,跳转登录窗口
                    AutoUpdateCoeus.CloseWindowDelay(1000);
                    Process.Start(Environment.CurrentDirectory + "\\" + AutoUpdateCoeus.LocalExeFileName, null);
                    return;
                }
                Dictionary <string, string> localSoftFileHashInfo = GetFileHashInfo(Environment.CurrentDirectory, Environment.CurrentDirectory + @"\" + AutoUpdateCoeus.TempUpgradeDirectory);
                Dictionary <string, string> localTempFileHashInfo = GetFileHashInfo(Environment.CurrentDirectory + @"\" + AutoUpdateCoeus.TempUpgradeDirectory, string.Empty);
                long tempCopyTotalSize = 0;
                foreach (var loopChild in serverDirectoryAndFileInfo.ChildList)
                {
                    loopChild.NeedDownload = true;
                }
                foreach (var loopServerFile in serverAllFileInfo.ChildList)
                {
                    loopServerFile.NeedDownload = !AutoUpdateCoeus.SelfFiles.Contains(loopServerFile.Name);

                    if (localSoftFileHashInfo.ContainsKey(loopServerFile.Key))
                    {
                        if (localSoftFileHashInfo[loopServerFile.Key] == loopServerFile.Hash)
                        {
                            loopServerFile.NeedDownload = false;
                        }
                    }
                    if (loopServerFile.NeedDownload)
                    {
                        if (localTempFileHashInfo.ContainsKey(loopServerFile.Key))
                        {
                            if (localTempFileHashInfo[loopServerFile.Key] == loopServerFile.Hash)
                            {
                                loopServerFile.NeedDownload = false;
                                tempCopyTotalSize          += loopServerFile.Size;
                            }
                        }
                    }
                    if (loopServerFile.NeedDownload)
                    {
                        _downloadTotalSize += loopServerFile.Size;
                        tempCopyTotalSize  += loopServerFile.Size;
                    }
                }

                if (_downloadTotalSize > 0 || tempCopyTotalSize > 0)
                {
                    if (_downloadTotalSize > 0)
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.lblProgressVaule.Text = Math.Round((decimal)_hasDownloadSize / 1024 / 1024, 2) + "/" +
                                                         Math.Round((decimal)_downloadTotalSize / 1024 / 1024, 2) + "M";
                        }));

                        ShowInfo("开始升级...");
                        _isDownloading = true;
                        _downLoadFileTimer.Start();
                        DownLoadFile(appPath, tmpDir, AutoUpdateCoeus.UserNameOfUpgrade, AutoUpdateCoeus.PasswordOfUpgrade,
                                     AutoUpdateCoeus.UrlOfUpgrade, serverDirectoryAndFileInfo.ChildList);
                        _isDownloading = false;
                    }

                    ShowInfo("复制文件到软件目录...");
                    CopyDir(tmpDir, appPath);
                    ShowInfo("复制文件到软件目录完成!");
                    if (Directory.Exists(tmpDir))
                    {
                        try
                        {
                            Directory.Delete(tmpDir, true);
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                    ShowInfo("升级成功!");
                }
                else
                {
                    ShowInfo("没有需要更新的文件。");
                }

                //升级成功之后修改本地的版本号
                string localConfigFilePath = LocalConfigFile.ConfigFilePath.SoftVersionNo.Replace(PH_SOFTVERSIONNO, null);
                if (!File.Exists(localConfigFilePath))
                {
                    string localConfigDirectory = appPath + "\\LocalConfig";
                    if (!Directory.Exists(localConfigDirectory))
                    {
                        Directory.CreateDirectory(localConfigDirectory);
                    }
                    string sysConDataDirectory = appPath + "\\LocalConfig\\SysConData";
                    if (!Directory.Exists(sysConDataDirectory))
                    {
                        Directory.CreateDirectory(sysConDataDirectory);
                    }
                    File.Create(localConfigFilePath);
                }
                if (!string.IsNullOrEmpty(AutoUpdateCoeus.LatestSoftVersionNo))
                {
                    WritePrivateProfileString(LocalConfigFile.SelectionName.SoftInfo,
                                              LocalConfigFile.KeyName.SoftVersionNo, AutoUpdateCoeus.LatestSoftVersionNo, localConfigFilePath);
                }
                //UpdateLocalVersionNo();

                upgradeResult = true;

                Process.Start(Environment.CurrentDirectory + "\\" + AutoUpdateCoeus.LocalExeFileName, null);

                //等待两秒钟之后关闭当前窗口,跳转登录窗口
                AutoUpdateCoeus.CloseWindowDelay(1000);
            }
            catch (Exception ex)
            {
                if (upgradeResult)
                {
                    Environment.Exit(0);
                }
                WriteLogFileAndEmail(ex);
                _isDownloading = false;
                if (tryTimes < 3)
                {
                    tryTimes++;
                    InitViewAndUpdateSoft();
                }
                return;
            }
            //Application.Exit();



            //string tmpDir = appPath + "\\" + AutoUpdateCoeus.TempUpgradeDirectory;
            //bool upgradeResult = false;
            //try
            //{
            //    if (!Directory.Exists(tmpDir))
            //    {
            //        Directory.CreateDirectory(tmpDir);
            //    }

            //    _hasDownloadSize = 0;
            //    _downloadTotalSize = 0;
            //    //totalSize = 0;
            //    ShowInfo("获取升级包大小...");
            //    downLoadFileNameList.Clear();

            //    GetDownloadFileSize(appPath, AutoUpdateCoeus.UserNameOfUpgrade, AutoUpdateCoeus.PasswordOfUpgrade, AutoUpdateCoeus.UrlOfUpgrade, downLoadFileNameList);
            //    ShowInfo("开始升级...");
            //    _isDownloading = true;
            //    _downLoadFileTimer.Start();
            //    DownLoadFile(appPath, tmpDir, AutoUpdateCoeus.UserNameOfUpgrade, AutoUpdateCoeus.PasswordOfUpgrade, AutoUpdateCoeus.UrlOfUpgrade, downLoadFileNameList);
            //    _isDownloading = false;
            //    ShowInfo("复制文件到工作目录...");
            //    CopyDirection(tmpDir, appPath);
            //    ShowInfo("复制文件到工作目录完成!!!");
            //    Directory.Delete(tmpDir, true);
            //    ShowInfo("升级成功!!!");

            //    //升级成功之后修改本地的版本号
            //    string localConfigFilePath = LocalConfigFile.ConfigFilePath.SoftVersionNo.Replace(PH_SOFTVERSIONNO, null);
            //    if (!string.IsNullOrEmpty(AutoUpdateCoeus.LatestSoftVersionNo))
            //    {
            //        WritePrivateProfileString(LocalConfigFile.SelectionName.SoftInfo, LocalConfigFile.KeyName.SoftVersionNo, AutoUpdateCoeus.LatestSoftVersionNo, localConfigFilePath);
            //    }
            //    //UpdateLocalVersionNo();
            //    AutoUpdateCoeus.CloseWindowDelay(2000);
            //    //等待两秒钟之后关闭当前窗口,跳转登录窗口
            //    Process.Start(Environment.CurrentDirectory + "\\SkyCar.Coeus.Ult.Entrance.exe", null);
            //}
            //catch (Exception ex)
            //{
            //    WriteLogFileAndEmail(ex);
            //    _isDownloading = false;
            //    Directory.Delete(tmpDir, true);
            //    MessageBox.Show(ex.Message);
            //    return;
            //}
            ////Application.Exit();
        }
Example #4
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="currentWorkPath"></param>
        /// <param name="localPath"></param>
        /// <param name="ftpUser"></param>
        /// <param name="ftpPasswd"></param>
        /// <param name="ftpPath"></param>
        private void DownLoadFile(string currentWorkPath, string localPath, string ftpUser, string ftpPasswd, string ftpPath, List <FTPFileInfo> fileList)
        {
            string curFileName = string.Empty;

            try
            {
                foreach (var fileInfo in fileList)
                {
                    curFileName = fileInfo.Name;
                    if (fileInfo.IsDirectory)
                    {
                        if (fileInfo.ChildList.Count > 0)
                        {
                            string newFtpPath = (ftpPath.EndsWith("/")
                                ? ftpPath + fileInfo.Name + "/"
                                : ftpPath + "/" + fileInfo.Name + "/");
                            string newLocalPath       = localPath + "\\" + fileInfo.Name;
                            string newCurrentWorkPath = currentWorkPath + "\\" + fileInfo.Name;
                            if (!Directory.Exists(newLocalPath))
                            {
                                Directory.CreateDirectory(newLocalPath);
                            }
                            DownLoadFile(newCurrentWorkPath, newLocalPath, ftpUser, ftpPasswd, newFtpPath,
                                         fileInfo.ChildList);
                        }
                    }
                    else if (fileInfo.NeedDownload)
                    {
                        if (fileInfo.Name == "Newtonsoft.Json.dll")
                        {
                        }
                        ShowInfo(string.Format("正在下载文件{0}...", fileInfo.Name));
                        int count = 0;
                        while (true)
                        {
                            try
                            {
                                FTPClientHelper.Instance.Download(ftpUser, ftpPasswd, ftpPath, localPath, fileInfo.Name);
                                _hasDownloadSize += fileInfo.Size;
                                break;
                            }
                            catch (Exception ex)
                            {
                                AutoUpdateCoeus.WriteLogFileAndEmail(ex, "下载" + fileInfo.Name + "时发现异常");
                                count++;
                                if (count <= 3)
                                {
                                    ShowInfo(string.Format("下载文件{0}失败!!!失败原因:{1},正在进行第{2}次重试...", fileInfo.Name,
                                                           ex.Message, count));
                                    continue;
                                }
                                else
                                {
                                    ShowInfo(string.Format("下载文件{0}失败!!!失败原因:{1}", fileInfo.Name, ex.Message));
                                    throw ex;
                                }
                            }
                        }
                        ShowInfo(string.Format("下载文件{0}完成", fileInfo.Name));
                    }
                }
            }
            catch (Exception ex)
            {
                AutoUpdateCoeus.WriteLogFileAndEmail(ex, "下载" + curFileName + "时发现异常");
                throw;
            }
        }
Example #5
0
        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="currentWorkPath">工作路径</param>
        /// <param name="ftpUser">FTP用户名</param>
        /// <param name="ftpPasswd">FTP密码</param>
        /// <param name="ftpPath">FTP的路径</param>
        private void GetDownloadFileSize(string currentWorkPath, string ftpUser, string ftpPasswd, string ftpPath,
                                         List <FTPFileInfo> fileList, string folderName = "")
        {
            string tempUpgradeDicectory = appPath + "\\" + AutoUpdateCoeus.TempUpgradeDirectory;

            string[]      fileFullPathList = FTPClientHelper.Instance.GetDetailFilePath(ftpUser, ftpPasswd, ftpPath);
            List <string> fileNameList     =
                new List <string>(FTPClientHelper.Instance.GetFilePath(ftpUser, ftpPasswd, ftpPath));

            foreach (var fileFullPath in fileFullPathList)
            {
                string onlyFileName = string.Empty;
                foreach (var fileName in fileNameList)
                {
                    if (fileFullPath.Contains(fileName))
                    {
                        onlyFileName = fileName;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(onlyFileName) && !AutoUpdateCoeus.SelfFiles.Contains(onlyFileName))
                {
                    if (fileFullPath.TrimStart().StartsWith("d"))
                    {
                        string newFtpPath = (ftpPath.EndsWith("/")
                            ? ftpPath + onlyFileName + "/"
                            : ftpPath + "/" + onlyFileName + "/");
                        string      newCurrentWorkPath = currentWorkPath + "\\" + onlyFileName;
                        FTPFileInfo fileInfo           = new FTPFileInfo
                        {
                            Name        = onlyFileName,
                            IsDirectory = true,
                            ChildList   = new List <FTPFileInfo>()
                        };
                        fileList.Add(fileInfo);
                        GetDownloadFileSize(newCurrentWorkPath, ftpUser, ftpPasswd, newFtpPath, fileInfo.ChildList,
                                            (!string.IsNullOrEmpty(folderName) ? folderName + "\\" : string.Empty) + onlyFileName);
                    }
                    else
                    {
                        DateTime ftpFileModifyTime = FTPClientHelper.Instance.GetFileModifyDateTime(ftpUser, ftpPasswd,
                                                                                                    ftpPath, string.Empty, onlyFileName);
                        DateTime localFileModifyTime = ftpFileModifyTime.AddYears(-1);
                        string   localFullFileName   = currentWorkPath + "\\" + onlyFileName;
                        if (File.Exists(localFullFileName))
                        {
                            localFileModifyTime = File.GetLastWriteTime(localFullFileName);
                        }
                        //程序路径不存在文件,判断是否存在升级临时文件
                        if (localFileModifyTime < ftpFileModifyTime)
                        {
                            string tempFullFileName = tempUpgradeDicectory +
                                                      (!string.IsNullOrEmpty(folderName)
                                                          ? "\\" + folderName
                                                          : string.Empty) + "\\" + onlyFileName;
                            if (File.Exists(tempFullFileName))
                            {
                                localFileModifyTime = File.GetLastWriteTime(tempFullFileName);
                            }
                        }
                        if (onlyFileName.Contains("programConfig") || localFileModifyTime < ftpFileModifyTime)
                        {
                            int count = 0;
                            while (true)
                            {
                                try
                                {
                                    var tempList = !string.IsNullOrEmpty(fileFullPath)
                                        ? fileFullPath.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList()
                                        : null;
                                    //totalSize += FTPClientHelper.Instance.GetFileSize(ftpUser, ftpPasswd, ftpPath, onlyFileName);
                                    if (tempList != null)
                                    {
                                        _downloadTotalSize += long.Parse(tempList[4]);
                                    }
                                    this.Invoke(new Action(() =>
                                    {
                                        this.lblProgressVaule.Text =
                                            Math.Round((decimal)_hasDownloadSize / 1024 / 1024, 2) + "/" +
                                            Math.Round((decimal)_downloadTotalSize / 1024 / 1024, 2) + "M";
                                    }));
                                    fileList.Add(new FTPFileInfo
                                    {
                                        IsDirectory = false,
                                        Name        = onlyFileName,
                                        Size        = long.Parse(tempList[4])
                                    });
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    AutoUpdateCoeus.WriteLogFileAndEmail(ex, "获取" + onlyFileName + "大小时发现异常");
                                    count++;
                                    if (count <= 3)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        throw ex;
                                    }
                                }
                            }
                        }
                    }
                    fileNameList.Remove(onlyFileName);
                }
            }
        }