

























http://www.vjsdn.com/bbs/bbsTopicDetails.aspx?pid=597
续上项目介绍及UML图解
版本自动更新程序及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; } }//下载失败的文件总数
}
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; } }//下载失败的文件总数
}
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; } }//下载失败的文件总数
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。