惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Securelist
V2EX - 技术
V2EX - 技术
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
Spread Privacy
Spread Privacy
N
News and Events Feed by Topic
H
Heimdal Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
L
LangChain Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
G
Google Developers Blog
Recorded Future
Recorded Future
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The GitHub Blog
The GitHub Blog
量子位
V
V2EX
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Vercel News
Vercel News
H
Help Net Security
Know Your Adversary
Know Your Adversary
Forbes - Security
Forbes - Security
T
Threatpost
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
罗磊的独立博客
C
Check Point Blog
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
A
Arctic Wolf

博客园 - 海浪~~

[转]Visual Studio 2008 每日提示系列索引 取消“解决方案资源管理器”自动跟随 【转】ie6.0下div 不能实现1px 高度的几种解决方法 - 海浪~~ 【转】IE6下div边框显示 不完整有残缺 【转】IE6下IMG下出现空白条的解决方法汇总 【转】enter键转tab键 (支持ie,ff,chrome) - 海浪~~ - 博客园 【转】安装 SQL Server 2008 的硬件和软件要求 【转】sql语句优化工具LECCO SQL Expert 版本自动更新程序及3种实现策略程序下载(附源码) 版本自动更新程序及3种实现策略-分析文档(UML图) SQL SERVER 获取表结构信息《转载》 某知名公司ERP数据库结构[6] 某知名公司ERP数据库结构[5] 某知名公司ERP数据库结构[4] 某知名公司ERP数据库结构[3] 某知名公司ERP数据库结构[2] 某知名公司ERP数据库结构[1] 关于VS2008调试时端口号不一致的问题(ZT) [导入]Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载) [导入]Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载) [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载) [导入]AjaxPro.NET框架生成高效率的Tree(Asp.net 2.0)(示例代码下载) [导入]AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载) [导入]Asp.net 2.0 制作最原始的TextBox控件[一](示例代码下载) [导入]Asp.net 2.0 一个简单的联动DropDownList示例(示例代码下载) [导入]ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载) [导入]ASP.NET 2.0 读取配置文件[INI](示例代码下载) [导入]Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载) [导入]Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载) [导入]Asp.net 2.0 制作复合控件示例(二)[示例代码下载] [导入]Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载) [导入]JavaScript组件之JQuery(A~Z)教程(基于Asp.net运行环境)[示例代码下载]
版本自动更新程序及3种实现策略(二)下载器实现
海浪~~ · 2009-09-25 · via 博客园 - 海浪~~

http://www.vjsdn.com/bbs/bbsTopicDetails.aspx?pid=597


续上项目介绍及UML图解
版本自动更新程序及3种实现策略
(一)
 

版本自动更新程序及3种实现策略程序下载(附源码)

下面是三种下载器代码实现

局域网复制文件下载器

 
 
 
public class LAN_Downloader : IDownloader
{
   private XmlLoader _serverXmlObj;
   private XmlLoader _clientXmlObj;
   private bool _inited = false
   
   public XmlLoader XmlServer { get { return _serverXmlObj; } }
   public XmlLoader XmlLocal { get { return _clientXmlObj; } }
    
   
   public void Init() //初始化下载器
   {
       
      string serverXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
       
      string clientXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ClientFilesXml;
      
      this.DownloadServerXml();  
      
      if (!File.Exists(clientXml)) XmlLoader.CreateEmpty(clientXml);
      
      _serverXmlObj = new XmlLoader(serverXml);  
      _clientXmlObj = new XmlLoader(clientXml); 
      
      _inited = true;
   }
   
   public void DownloadServerXml()
   {
       
      string serverXml = AppSettings.Instance.ServerSharedFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
       
      string serverXml_Local = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
      if (File.Exists(serverXml_Local)) File.Delete(serverXml_Local);
      
      File.Copy(serverXml, serverXml_Local); 
   }
   
    
   public void Download()
   {
      if (false == _inited) return
      
      IList files = _serverXmlObj.GetFiles();
      _progress.Maximum = files.Count + 1;
      _progress.Minimum = 1;
      _progress.Value = 1;
      foreach (FileInfo file in files)
      {
         bool update = _clientXmlObj.CompareFile(file);  
         if (update)
         {
            FileInfo local = this.DownloadFile(file);
            if (local != null)
            {
               _clientXmlObj.AddOrUpdateHistory(file, local);
               _DownloadsCount++; 
            }
            else
            {
               _DownloadFaliedCount++; 
            }
         }
         _progress.Value += 1;
      }
      
      _clientXmlObj.SetLastUpdateInfo(_serverXmlObj.GetVersion(), DateTime.Now);
      _clientXmlObj.Save(); 
   }
   
   private ToolStripProgressBar _progress = null;
   public void SetProgressBar(ToolStripProgressBar progress)
   {
      _progress = progress;
   }
   
   private ListBox _logList = null;
   public void SetTrace(ListBox logList)
   {
      _logList = logList;
   }
   
    
    
    
   public FileInfo DownloadFile(FileInfo file)
   {
      try
      {
         string folder = AppSettings.Instance.ClientSaveFolder;
         string shared = AppSettings.Instance.ServerSharedFolder;
         
         string from = shared + file.FullPath.Replace(@".\", @"\");
         string dest = folder + file.FullPath.Replace(@".\", @"\");
         
         PrepareFile(dest); 
         
         File.Copy(from, dest);  
         
         if (_logList != null) _logList.Items.Add("已下载文件:" + file.FullPath);
         
          
         return new FileInfo(file.Name, from, file.ModifyTime);
      }
      catch
      {
         return null;
      }
   }
   
   private void PrepareFile(string file)
   {
      if (file.Trim() == "") return;
      string dir = Path.GetDirectoryName(file);
      if (!Directory.Exists(dir)) //目录不存在-建立目录
      Directory.CreateDirectory(dir);
      else //文件存在-删除文件.
      if (File.Exists(file)) File.Delete(file);
   }
   
   private int _DownloadsCount = 0;
   public int DownloadsCount { get { return _DownloadsCount; } } //下载成功的文件总数
   
   private int _DownloadFaliedCount = 0;
   public int DownloadFaliedCount { get { return _DownloadFaliedCount; } }//下载失败的文件总数
}


WebClient组件下载器

 
 
 
public class WebClient_Downloader : IDownloader
{
   private WebClient _webClient;
   private XmlLoader _serverXmlObj;
   private XmlLoader _clientXmlObj;
   private bool _inited = false
   
   public XmlLoader XmlServer { get { return _serverXmlObj; } }
   public XmlLoader XmlLocal { get { return _clientXmlObj; } }
   
   public void Init() //初始化下载器
   {
      _webClient = new WebClient();
      
       
      string serverXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
       
      string clientXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ClientFilesXml;
      
      this.DownloadServerXml();  
      
      if (!File.Exists(clientXml)) XmlLoader.CreateEmpty(clientXml);
      
      _serverXmlObj = new XmlLoader(serverXml);  
      _clientXmlObj = new XmlLoader(clientXml); 
      
      _inited = true;
   }
   
   
   public FileInfo DownloadFile(FileInfo file) //下载单个文件
   {
      try
      {
         string folder = AppSettings.Instance.ClientSaveFolder;
         string url = AppSettings.Instance.ServerPublishUrl;
         
         string from = url + file.FullPath.Replace(@".\", @"/");
         string dest = folder + file.FullPath.Replace(@".\", @"\");
         
         PrepareFile(dest); 
         
          
         byte[] bs = _webClient.DownloadData(from);
         FileStream fs = File.Open(dest, FileMode.OpenOrCreate, FileAccess.Write);
         fs.Write(bs, 0, bs.Length);
         fs.Flush();
         fs.Close();
         
         if (_logList != null) _logList.Items.Add("已下载文件:" + file.FullPath);
         
          
         return new FileInfo(file.Name, from, file.ModifyTime);
      }
      catch
      {
         return null;
      }
   }
   
   private void PrepareFile(string file)
   {
      if (file.Trim() == "") return;
      string dir = Path.GetDirectoryName(file);
      if (!Directory.Exists(dir)) //目录不存在-建立目录
      Directory.CreateDirectory(dir);
      else //文件存在-删除文件.
      if (File.Exists(file)) File.Delete(file);
   }
   
    
   public void Download()
   {
      if (false == _inited) return
      
      IList files = _serverXmlObj.GetFiles();
      _progress.Maximum = files.Count + 1;
      _progress.Minimum = 1;
      _progress.Value = 1;
      foreach (FileInfo file in files)
      {
         bool update = _clientXmlObj.CompareFile(file);  
         if (update)
         {
            FileInfo local = this.DownloadFile(file);
            if (local != null)
            {
               _clientXmlObj.AddOrUpdateHistory(file, local);
               _DownloadsCount++; 
            }
            else
            {
               _DownloadFaliedCount++; 
            }
         }
         
         _progress.Value += 1;
      }
      _clientXmlObj.SetLastUpdateInfo(_serverXmlObj.GetVersion(), DateTime.Now);
      _clientXmlObj.Save(); 
   }
   
   public void DownloadServerXml()
   {
       
      string serverXml = AppSettings.Instance.ServerPublishUrl + "/" + AppSettings.Instance.ServerFilesXml;
      
       
      string serverXml_Local = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
      if (File.Exists(serverXml_Local)) File.Delete(serverXml_Local);
      
      WebClient client = new WebClient();
      
      client.DownloadFile(serverXml, serverXml_Local);
   }
   
   private ToolStripProgressBar _progress = null;
   public void SetProgressBar(ToolStripProgressBar progress)
   {
      _progress = progress;
   }
   
   private ListBox _logList = null;
   public void SetTrace(ListBox logList)
   {
      _logList = logList;
   }
   
   private int _DownloadsCount = 0;
   public int DownloadsCount { get { return _DownloadsCount; } } //下载成功的文件总数
   
   private int _DownloadFaliedCount = 0;
   public int DownloadFaliedCount { get { return _DownloadFaliedCount; } }//下载失败的文件总数
}


Tcp/IP下载器

 

 
public class TcpIp_Downloader : IDownloader
{
   private UpgraderClient _UpgraderClient;
   
   private XmlLoader _serverXmlObj;
   private XmlLoader _clientXmlObj;
   private bool _inited = false
   
   public XmlLoader XmlServer { get { return _serverXmlObj; } }
   public XmlLoader XmlLocal { get { return _clientXmlObj; } }
   
   public void Init() //初始化下载器
   {
      _UpgraderClient = new UpgraderClient(null);
      
       
      string serverXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ServerFilesXml;
      
       
      string clientXml = AppSettings.Instance.ClientSaveFolder + "\\" + AppSettings.Instance.ClientFilesXml;
      
      this.DownloadServerXml();  
      
      if (!File.Exists(clientXml)) XmlLoader.CreateEmpty(clientXml);
      
      _serverXmlObj = new XmlLoader(serverXml);  
      _clientXmlObj = new XmlLoader(clientXml); 
      
      _inited = true;
   }
   
   public FileInfo DownloadFile(FileInfo file) //下载单个文件
   {
      try
      {
         string from = AppSettings.Instance.ServerPublishFolder + file.FullPath.Replace(@".\", @"\");
         string dest = AppSettings.Instance.ClientSaveFolder + file.FullPath.Replace(@".\", @"\");
         
         PrepareFile(dest); 
         
         bool ret = _UpgraderClient.DownloadFile(from, dest);  
         
          
         if (ret)
         {
            if (_logList != null) _logList.Items.Add("已下载文件:" + file.FullPath);
            return new FileInfo(file.Name, from, file.ModifyTime);
         }
         else
         {
            if (_logList != null) _logList.Items.Add("下载文件:" + file.FullPath + "失败!");
            return null;
         }
      }
      catch
      {
         return null;
      }
   }
   
    
   public void Download()
   {
      if (false == _inited) return
      
      IList files = _serverXmlObj.GetFiles();
      _progress.Maximum = files.Count + 1;
      _progress.Minimum = 1;
      _progress.Value = 1;
      foreach (FileInfo file in files)
      {
         bool update = _clientXmlObj.CompareFile(file);  
         if (update)
         {
            FileInfo local = this.DownloadFile(file);
            if (local != null)
            {
               _clientXmlObj.AddOrUpdateHistory(file, local);
               _DownloadsCount++; 
            }
            else
            {
               _DownloadFaliedCount++; 
            }
         }
         
         _progress.Value += 1;
      }
      
      _clientXmlObj.SetLastUpdateInfo(_serverXmlObj.GetVersion(), DateTime.Now);
      _clientXmlObj.Save(); 
   }
   
   public void DownloadServerXml()
   {
       
      string serverXml = AppSettings.Instance.ServerPublishFolder + @"\" + AppSettings.Instance.ServerFilesXml;
      
       
      string serverXml_Local = AppSettings.Instance.ClientSaveFolder + @"\" + AppSettings.Instance.ServerFilesXml;
      
      if (File.Exists(serverXml_Local)) File.Delete(serverXml_Local);
      
      bool ret = _UpgraderClient.DownloadFile(serverXml, serverXml_Local); 
      if (false == ret) throw new Exception("不能下载文件列表!\n可能是xml文件不存在或没有启动tcp/ip服务!");
   }
   
   private ToolStripProgressBar _progress = null;
   public void SetProgressBar(ToolStripProgressBar progress)
   {
      _progress = progress;
   }
   
   
   private void PrepareFile(string file)
   {
      if (file.Trim() == "") return;
      string dir = Path.GetDirectoryName(file);
      if (!Directory.Exists(dir)) //目录不存在-建立目录
      Directory.CreateDirectory(dir);
      else //文件存在-删除文件.
      if (File.Exists(file)) File.Delete(file);
   }
   
   private ListBox _logList = null;
   public void SetTrace(ListBox logList)
   {
      _logList = logList;
   }
   
   private int _DownloadsCount = 0;
   public int DownloadsCount { get { return _DownloadsCount; } } //下载成功的文件总数
   
   private int _DownloadFaliedCount = 0;
   public int DownloadFaliedCount { get { return _DownloadFaliedCount; } }//下载失败的文件总数
}