Downloader调用WCF服务返回文件
Generator
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 5 namespace Downloader 6 { 7 public class Generator : StatusProcess 8 { 9 private readonly string _appPath = AppDomain.CurrentDomain.BaseDirectory; 10 protected void GenerateFileList(string dir, List<FileEntity> fileEntities) 11 { 12 var files = Directory.GetFiles(dir); 13 foreach (var tem in files) 14 { 15 var fileInfo = new FileInfo(tem); 16 var file = new FileEntity() 17 { 18 FileName = tem.Replace(_appPath, "").Replace("\\", "\\\\"), 19 LastUpdate = fileInfo.LastWriteTime.ToString("yyyyMMddHHmmss") 20 }; 21 fileEntities.Add(file); 22 } 23 24 var directories = Directory.GetDirectories(dir); 25 foreach (var directory in directories) 26 { 27 GenerateFileList(directory, fileEntities); 28 } 29 } 30 31 public void GenerateFileList() 32 { 33 var filelist = new FileListEntity() { FileCode = Guid.NewGuid().ToString().ToUpper().Replace("-", "") }; 34 GenerateFileList(_appPath, filelist.FileEntities); 35 filelist.SerializeConfig(Path.Combine(_appPath, "filelist.xml")); 36 } 37 38 public void DownloadFileList(string path, string url, string customerCode, string token, string mac) 39 { 40 #region 请求filelist.xml 41 42 var client = new RestClient 43 { 44 EndPoint = url, 45 Method = HttpVerb.Post 46 }; 47 48 const string curfilename = "curfilelist.xml"; 49 const string newfilename = "filelist.xml"; 50 InvokeStatus(string.Format("正在请求{0}", newfilename)); 51 string filename = newfilename; 52 string postdata = 53 string.Format("\"CustomerCode\":\"{0}\",\"Token\":\"{1}\",\"Mac\":\"{2}\",\"Filename\":\"{3}\"", 54 customerCode, token, mac, filename); 55 var makeRequest = client.MakeRequest("", postdata); 56 57 #endregion 58 59 if (makeRequest.Length > 0) 60 { 61 InvokeStatus(string.Format("正在保存{0}", newfilename)); 62 63 #region 正在保存filelist.xml 64 65 filename = Path.Combine(path, filename); 66 var directoryName = Path.GetDirectoryName(filename); 67 if (!Directory.Exists(directoryName)) 68 { 69 Directory.CreateDirectory(directoryName); 70 } 71 using (var file = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite)) 72 { 73 file.Write(makeRequest, 0, makeRequest.Length); 74 } 75 76 #endregion 77 78 #region 判断是否需要更新 79 80 var curfileListEntity = new FileListEntity(); 81 var fileListEntity = filename.DeserializeConfig<FileListEntity>(); 82 83 if (File.Exists(Path.Combine(path, curfilename))) 84 { 85 curfileListEntity = Path.Combine(path, curfilename).DeserializeConfig<FileListEntity>(); 86 if (fileListEntity.FileCode == curfileListEntity.FileCode) 87 { 88 File.Delete(Path.Combine(path, newfilename)); 89 InvokeStatus("不需要更新"); 90 return; 91 } 92 } 93 94 #endregion 95 96 #region 更新文件列表 97 98 foreach (var fileEntity in fileListEntity.FileEntities) 99 { 100 var find = curfileListEntity.FileEntities.Find(entity => entity.FileName == fileEntity.FileName); 101 102 if (find != null && fileEntity.LastUpdate == find.LastUpdate) 103 { 104 continue; 105 } 106 filename = fileEntity.FileName; 107 InvokeStatus("正在更新" + filename); 108 postdata = string.Format("\"CustomerCode\":\"{0}\",\"Token\":\"{1}\",\"Mac\":\"{2}\",\"Filename\":\"{3}\"", customerCode, token, mac, filename); 109 makeRequest = client.MakeRequest("", postdata); 110 if (makeRequest.Length > 0) 111 { 112 InvokeStatus("正在保存" + filename); 113 filename = Path.Combine(path, filename); 114 directoryName = Path.GetDirectoryName(filename); 115 if (!Directory.Exists(directoryName)) 116 { 117 Directory.CreateDirectory(directoryName); 118 } 119 using (var file = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite)) 120 { 121 file.Write(makeRequest, 0, makeRequest.Length); 122 } 123 } 124 } 125 126 #endregion 127 128 File.Delete(Path.Combine(path, curfilename)); 129 File.Move(Path.Combine(path, newfilename), Path.Combine(path, curfilename)); 130 InvokeStatus("update ok"); 131 } 132 else 133 { 134 InvokeStatus("请求更新失败,请确认配置信息"); 135 } 136 } 137 } 138 139 public class FileEntity 140 { 141 public string LastUpdate { get; set; } 142 public string FileName { get; set; } 143 } 144 145 public class FileListEntity 146 { 147 private List<FileEntity> _fileEntities = new List<FileEntity>(); 148 public string FileCode { get; set; } 149 150 public List<FileEntity> FileEntities 151 { 152 get { return _fileEntities; } 153 set { _fileEntities = value; } 154 } 155 } 156 }
StatusProcess
1 /* Jonney Create 2015-3-22 */ 2 using System.Diagnostics; 3 using System.Windows.Forms; 4 5 namespace Downloader 6 { 7 public delegate void DownloadStatusHandler(string status); 8 9 public class StatusProcess 10 { 11 /// <summary> 12 /// 下载状态事件,状态改变都会触发 13 /// </summary> 14 public event DownloadStatusHandler DownloadStatus; 15 16 protected readonly Stopwatch Stopwatch; 17 18 public StatusProcess() 19 { 20 Stopwatch = new Stopwatch(); 21 } 22 23 /// <summary> 24 /// 如果DownloadStatus事件被注册,都会被执行 25 /// </summary> 26 /// <param name="status"></param> 27 protected void InvokeStatus(string status) 28 { 29 if (DownloadStatus != null) 30 { 31 DownloadStatus.Invoke(status); 32 Application.DoEvents(); 33 } 34 } 35 } 36 37 38 }
Downloader
1 private void btnUpdater_Click(object sender, EventArgs e) 2 { 3 var thread = new Thread(DownloadFilelist) {IsBackground = true}; 4 thread.Start(); 5 } 6 7 private void DownloadFilelist() 8 { 9 try 10 { 11 SetEnable(false); 12 const string cfg = "CustomerInfo.xml"; 13 var generator = new Generator(); 14 generator.DownloadStatus += generator_DownloadStatus; 15 16 string file = Path.Combine(Application.StartupPath, cfg); 17 if (!File.Exists(file)) 18 { 19 generator_DownloadStatus(string.Format("配置信息{0}损坏", cfg)); 20 return; 21 } 22 var customerInfo = file.DeserializeConfig<CustomerInfo>(); 23 if (customerInfo == null) 24 { 25 generator_DownloadStatus(string.Format("配置信息{0}损坏", cfg)); 26 return; 27 } 28 29 var mac = GetMacString(); 30 customerInfo.Mac = mac[0]; 31 generator.DownloadFileList(Application.StartupPath 32 , customerInfo.ServerUrl 33 , customerInfo.CustomerCode 34 , customerInfo.Token 35 , customerInfo.Mac); 36 37 this.Invoke(new Action(Boot)); 38 } 39 catch (Exception err) 40 { 41 generator_DownloadStatus(string.Format("{0}", err.Message)); 42 } 43 finally 44 { 45 /*SetEnable(true);*/ 46 } 47 } 48 49 private void SetEnable(bool isEnable) 50 { 51 this.Invoke(new Action(() => 52 { 53 btnClose.Enabled = isEnable; 54 btnUpdater.Enabled = isEnable; 55 })); 56 } 57 58 private void generator_DownloadStatus(string status) 59 { 60 this.Invoke(new Action(() => 61 { 62 label1.Text = status; 63 Application.DoEvents(); 64 })); 65 } 66 67 public string[] GetMacString() 68 { 69 string strMac = ""; 70 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 71 foreach (NetworkInterface ni in interfaces) 72 { 73 if (ni.OperationalStatus == OperationalStatus.Up) 74 { 75 strMac += ni.GetPhysicalAddress() + "|"; 76 } 77 } 78 return strMac.Split('|'); 79 80 }
posted on 2016-07-31 23:24 jonney_wang 阅读(336) 评论(0) 编辑 收藏 举报