public DownloadParam(string name, string url, string path, RemoteConfigurationManager.DownloadChecker checker) { this.Name = name; this.Url = url; this.LocalPath = path; this.Checker = checker; }
private static bool Download(string resourceName, string url, string targetPath, RemoteConfigurationManager.DownloadChecker checker) { bool result; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Proxy = null; req.Method = "GET"; req.KeepAlive = false; HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); string tmpFile = RemoteConfigurationManager.GetTempFileName(targetPath); using (System.IO.Stream rspStream = rsp.GetResponseStream()) { try { if (checker != null) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { byte[] buf = new byte[4096]; int length; while ((length = rspStream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, length); } ms.Position = 0L; checker(resourceName, ms); ms.Position = 0L; RemoteConfigurationManager.WriteStreamToFile(ms, tmpFile); ms.Close(); goto IL_B5; } } RemoteConfigurationManager.WriteStreamToFile(rspStream, tmpFile); IL_B5: if (System.IO.File.Exists(targetPath)) { if (!RemoteConfigurationManager.Instance.config.BackupConfig) { System.IO.File.Delete(targetPath); } else { RemoteConfigurationManager.RemoveOldBackupFiles(RemoteConfigurationManager.GetSectionName(targetPath)); System.IO.File.Move(targetPath, RemoteConfigurationManager.GetBackupFileName(targetPath)); } } System.IO.File.Move(tmpFile, targetPath); } finally { System.IO.File.Delete(tmpFile); } } rsp.Close(); result = true; } catch (System.Exception ex) { BaseConfigurationManager.HandleException(ex, string.Concat(new string[] { "Unabled to download '", url, "' to '", targetPath, "'" }), resourceName); result = false; } return result; }