Ejemplo n.º 1
0
 /// <summary>
 /// 下载新版本
 /// </summary>
 /// <param name="repMessage">服务端返回的下载信息</param>
 public void DownloadVersion(ResponseMessage repMessage)
 {
     //写下载状态文件
     using (StreamWriter sw = new StreamWriter(_downloadDir + "\\DownloadStatus.txt", false))
     {
         sw.AutoFlush = true;
         sw.WriteLine("Downloading");
     }
     //获取版本管理文件
     XDocument xmlDoc = GetVersionXml();
     XElement downloadVersion = xmlDoc.Root.Element("DownloadedVersions");
     //如果在指定的时间内,下载未完成,向服务端发送消息
     _downloadStatus = DownLoadStaus.Downloading;
     int period = (repMessage.heartTime - 1) * 60 * 1000;//间隔时间(毫秒)
     Timer timer = new Timer(new TimerCallback(SendDownloadingMessage), _downloadingVersion, period, period);
     //执行下载            
     string ip = repMessage.downloadIP;
     VersionInfo[] versions = repMessage.allVersion;
     int curBag = 0;
     int bagNum = versions.Length;
     NamedPipeServerHelper.Start();
     NamedPipeServerHelper.Status = "Start";
     try
     {
         //如果是采用ftp方式下载
         if (ip.ToLower().Contains("ftp:"))
         {
             //按照服务端配置的格式解析下载地址
             string[] ipAddress = Regex.Split(ip, "##", RegexOptions.IgnoreCase);
             ip = ipAddress[0];
             string userName = null;
             string pw = null;
             if (ipAddress.Length > 1)
             {
                 userName = ipAddress[1];
                 pw = ipAddress[2];
             }
             FTPHelper ftp = new FTPHelper(userName, pw);
             foreach (VersionInfo v in versions)
             {
                 NamedPipeServerHelper.BagInfo = string.Format("总共{0}个包,正在下载第{1}个包...", bagNum, ++curBag);
                 _downloadingVersion = v.versionName;
                 //下载更新文件配置信息
                 ftp.Download(ip, _downloadDir, _downloadingVersion + ".xml");
                 //获取文件信息
                 long size = 0;
                 string description = "";
                 BeforeExecute befExe = null;
                 AfterExecute aftExe = null;
                 GetUpdateFileInfo(out size, out description, out befExe, out aftExe);
                 //下载资源包
                 ftp.Download(ip, _downloadDir, _downloadingVersion + ".zip", size);
                 //添加下载的版本信息到XML文件
                 AddVersionInfoToXML(xmlDoc, downloadVersion, v.isMust, description, befExe, aftExe);
                 //解压文件
                 GenerateZip.ZipHelper.UnZip(_downloadDir + "\\" + _downloadingVersion + ".zip", _downloadDir + "\\TempDir\\" + _downloadingVersion);
             }
         }
         //如果是采用Http方式下载
         else
         {
             foreach (VersionInfo v in versions)
             {
                 NamedPipeServerHelper.BagInfo = string.Format("总共{0}个包,正在下载第{1}个包...", bagNum, ++curBag);
                 _downloadingVersion = v.versionName;
                 //下载更新文件配置信息
                 HttpHelper.Download(ip, _downloadDir, _downloadingVersion + ".xml");
                 //获取文件信息
                 long size = 0;
                 string description = "";
                 BeforeExecute befExe = null;
                 AfterExecute aftExe = null;
                 GetUpdateFileInfo(out size, out description, out befExe, out aftExe);
                 //下载资源包
                 HttpHelper.Download(ip, _downloadDir, _downloadingVersion + ".zip", size);
                 //添加下载的版本信息到XML文件
                 AddVersionInfoToXML(xmlDoc, downloadVersion, v.isMust, description, befExe, aftExe);
                 //解压文件
                 GenerateZip.ZipHelper.UnZip(_downloadDir + "\\" + _downloadingVersion + ".zip", _downloadDir + "\\TempDir\\" + _downloadingVersion);
             }
         }
         using (StreamWriter sw = new StreamWriter(_downloadDir + "\\DownloadStatus.txt", false))
         {
             sw.AutoFlush = true;
             sw.WriteLine("Downloaded");
         }
         NamedPipeServerHelper.Status = "End";
         _downloadStatus = DownLoadStaus.Downloaded;
         OnDownloadCompleted(repMessage.maxVersion);
         //提示用户更新
         UpdatePrompt();
     }
     catch (Exception ex)
     {
         _loger.Error("DownloadVersion(ResponseMessage)方法:" + ex.Message);
         throw ex;
     }
     finally
     {
         if (timer != null)
         {
             timer.Dispose();
         }
         NamedPipeServerHelper.Close();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 下载新版本
        /// </summary>
        /// <param name="repMessage">服务端返回的下载信息</param>
        public void DownloadVersion(ResponseMessage repMessage)
        {
            //写下载状态文件
            using (StreamWriter sw = new StreamWriter(_downloadDir + "\\DownloadStatus.txt", false))
            {
                sw.AutoFlush = true;
                sw.WriteLine("Downloading");
            }
            //获取版本管理文件
            XDocument xmlDoc          = GetVersionXml();
            XElement  downloadVersion = xmlDoc.Root.Element("DownloadedVersions");

            //如果在指定的时间内,下载未完成,向服务端发送消息
            _downloadStatus = DownLoadStaus.Downloading;
            int   period = (repMessage.heartTime - 1) * 60 * 1000;//间隔时间(毫秒)
            Timer timer  = new Timer(new TimerCallback(SendDownloadingMessage), _downloadingVersion, period, period);
            //执行下载
            string ip = repMessage.downloadIP;

            VersionInfo[] versions = repMessage.allVersion;
            int           curBag   = 0;
            int           bagNum   = versions.Length;

            NamedPipeServerHelper.Start();
            NamedPipeServerHelper.Status = "Start";
            try
            {
                //如果是采用ftp方式下载
                if (ip.ToLower().Contains("ftp:"))
                {
                    //按照服务端配置的格式解析下载地址
                    string[] ipAddress = Regex.Split(ip, "##", RegexOptions.IgnoreCase);
                    ip = ipAddress[0];
                    string userName = null;
                    string pw       = null;
                    if (ipAddress.Length > 1)
                    {
                        userName = ipAddress[1];
                        pw       = ipAddress[2];
                    }
                    FTPHelper ftp = new FTPHelper(userName, pw);
                    foreach (VersionInfo v in versions)
                    {
                        NamedPipeServerHelper.BagInfo = string.Format("总共{0}个包,正在下载第{1}个包...", bagNum, ++curBag);
                        _downloadingVersion           = v.versionName;
                        //下载更新文件配置信息
                        ftp.Download(ip, _downloadDir, _downloadingVersion + ".xml");
                        //获取文件信息
                        long          size        = 0;
                        string        description = "";
                        BeforeExecute befExe      = null;
                        AfterExecute  aftExe      = null;
                        GetUpdateFileInfo(out size, out description, out befExe, out aftExe);
                        //下载资源包
                        ftp.Download(ip, _downloadDir, _downloadingVersion + ".zip", size);
                        //添加下载的版本信息到XML文件
                        AddVersionInfoToXML(xmlDoc, downloadVersion, v.isMust, description, befExe, aftExe);
                        //解压文件
                        GenerateZip.ZipHelper.UnZip(_downloadDir + "\\" + _downloadingVersion + ".zip", _downloadDir + "\\TempDir\\" + _downloadingVersion);
                    }
                }
                //如果是采用Http方式下载
                else
                {
                    foreach (VersionInfo v in versions)
                    {
                        NamedPipeServerHelper.BagInfo = string.Format("总共{0}个包,正在下载第{1}个包...", bagNum, ++curBag);
                        _downloadingVersion           = v.versionName;
                        //下载更新文件配置信息
                        HttpHelper.Download(ip, _downloadDir, _downloadingVersion + ".xml");
                        //获取文件信息
                        long          size        = 0;
                        string        description = "";
                        BeforeExecute befExe      = null;
                        AfterExecute  aftExe      = null;
                        GetUpdateFileInfo(out size, out description, out befExe, out aftExe);
                        //下载资源包
                        HttpHelper.Download(ip, _downloadDir, _downloadingVersion + ".zip", size);
                        //添加下载的版本信息到XML文件
                        AddVersionInfoToXML(xmlDoc, downloadVersion, v.isMust, description, befExe, aftExe);
                        //解压文件
                        GenerateZip.ZipHelper.UnZip(_downloadDir + "\\" + _downloadingVersion + ".zip", _downloadDir + "\\TempDir\\" + _downloadingVersion);
                    }
                }
                using (StreamWriter sw = new StreamWriter(_downloadDir + "\\DownloadStatus.txt", false))
                {
                    sw.AutoFlush = true;
                    sw.WriteLine("Downloaded");
                }
                NamedPipeServerHelper.Status = "End";
                _downloadStatus = DownLoadStaus.Downloaded;
                OnDownloadCompleted(repMessage.maxVersion);
                //提示用户更新
                UpdatePrompt();
            }
            catch (Exception ex)
            {
                _loger.Error("DownloadVersion(ResponseMessage)方法:" + ex.Message);
                throw ex;
            }
            finally
            {
                if (timer != null)
                {
                    timer.Dispose();
                }
                NamedPipeServerHelper.Close();
            }
        }