private static DownloadedEventArgs Download(string fileUrl) { DownloadedEventArgs args = new DownloadedEventArgs(); try { HttpWebRequest reqUrl = (HttpWebRequest)WebRequest.Create(fileUrl); using (HttpWebResponse respUrl = (HttpWebResponse)(WebResponse)reqUrl.GetResponse()) { using (BinaryReader br = new BinaryReader(respUrl.GetResponseStream())) { long fileLenth = respUrl.ContentLength; byte[] fileContent = br.ReadBytes((Int32)fileLenth); string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), Path.GetFileName(fileUrl)); BinaryWriter bw = new BinaryWriter(new FileStream(filePath, FileMode.Create)); bw.Write(fileContent, 0, (Int32)fileLenth); bw.Close(); args.Code = "OK"; args.Desc = "下载成功"; args.FilePath = filePath; } } } catch (Exception ex) { args.Exception = ex; } return(args); }
private static void DoWork() { CheckedEventArgs argsCheck = Check(Constants.CurVersion, Constants.UpdaterUrl); if (argsCheck.Exception != null) { if (null != OnEnd) { OnEnd(argsCheck.Exception, false); } return; } if (argsCheck.Code == "Unavailable") { if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = argsCheck.Desc, Percent = 100 }); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Progressing", Desc = argsCheck.Desc, Percent = 33 }); } DownloadedEventArgs argsDownload = Download(argsCheck.FileUrl); if (argsDownload.Exception != null) { if (null != OnEnd) { OnEnd(argsDownload.Exception, false); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Progressing", Desc = argsDownload.Desc, Percent = 66 }); } ResultEventArgs argsResult = null; if (argsDownload.FilePath.EndsWith(".zip")) { argsResult = UnZip(argsDownload.FilePath); } else if (argsDownload.FilePath.EndsWith(".cab")) { argsResult = ExecuteCab(argsDownload.FilePath); } if (null == argsResult) { if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = "升级成功", Percent = 100 }); } return; } if (argsResult.Exception != null) { if (null != OnEnd) { OnEnd(argsResult.Exception, false); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = argsResult.Desc, Percent = 100, NoRun = argsResult.NoRun }); } }