/// <summary> /// Download a File /// <param name="requestUriString"></param> /// </summary> /// <returns>Downloaded File Name</returns> public void Download(string url, string path) { MyUtil.CreateDir(path: path); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString: url); httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"; try { using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) { using (Stream st = httpWebResponse.GetResponseStream()) { using (Stream so = new FileStream(path: path, mode: FileMode.Create)) { byte[] by = new byte[2048]; int osize = st.Read(buffer: by, offset: 0, count: by.Length); while (osize > 0) { so.Write(buffer: by, offset: 0, count: osize); osize = st.Read(buffer: by, offset: 0, count: by.Length); } } } } } catch (Exception ex) { throw new Exception("MyHttp.Download() fail,error:" + ex.Message); } }
private static void Update() { if (File.Exists(path: Config.SHAFile)) { oldFileList = new ConcurrentDictionary <string, string>(); MyUtil.MakeFileinfo(SHA: Config.oldSHA, fileinfo: oldFileList); } errorList = new ConcurrentDictionary <string, string>(); All_num = fileList.Count; Num = 0; Download(); // 开始下载 if (errorList.Count > 0) { Console.WriteLine(value: "Some of files failed to update ... ..."); Console.WriteLine(value: "Please retry the update later."); MyUtil.SaveList(path: Config.errorFile, fileinfo: errorList); } else { // Save SHA Value MyUtil.CreateDir(path: Config.SHAFile); File.WriteAllText(path: Config.SHAFile, contents: Config.newSHA, encoding: Encoding.UTF8); Console.WriteLine(value: "Update success! Though keep this windows until [Press Any Key to continue ... ... ]."); } }