public static bool DownLoad(string url, string filename, fileinfo ff) { if (myhttplistiner != null) { myhttplistiner.OnStart(url, filename); } bool isOK = false; try { if (File.Exists(filename)) { File.Delete(filename); } else { MyUtil.createDir(filename); } HttpWebRequest Myrq = (HttpWebRequest)System.Net.HttpWebRequest.Create(url); Myrq.Timeout = 30000; //Myrq.UserAgent="Mozilla/5.0 (Windows NT 6.2; WOW64) " // +"AppleWebKit/537.36 (KHTML, like Gecko) " // +"Chrome/27.0.1453.94 Safari/537.36"; if (MyHttp.isProxy) { Myrq.Proxy = new WebProxy(MyHttp.proxyip, MyHttp.proxyport); } HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; Stream st = myrp.GetResponseStream(); Stream so = new System.IO.FileStream(filename + ".tmp", FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[2048]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); File.Delete(filename); File.Move(filename + ".tmp", filename); } catch (System.Exception) { isOK = false; } isOK = File.Exists(filename); if (myhttplistiner != null) { myhttplistiner.OnEnd(ff, isOK); } return(isOK); }
public static void saveText(string file, string str) { if (File.Exists(file)) { File.Delete(file); } else { MyUtil.createDir(file); } File.WriteAllText(file, str, Encoding.UTF8); }
public static void saveList(string file, fileinfo[] fileinfos) { if (File.Exists(file)) { File.Delete(file); } else { MyUtil.createDir(file); } using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)){ StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); if (fileinfos != null) { foreach (fileinfo ff in fileinfos) { sw.WriteLine(ff.name + "\t" + ff.md5); } } sw.Close(); } }