C#实现在winfrom程序中下载文件

//下载文件
//downlaodUrl 系统路径如:http://xxx.xxx.xxx/UpFile/kaoqin.doc
//fileName 自定义文件名字加后缀(如:考勤.doc)
//filePath 文件存放路径如: E:\2016\1\
public static void downfile(string downloadUrl,string filename,string filepath) { HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downloadUrl); hwr.Timeout = 15000; HttpWebResponse hwp = (HttpWebResponse)hwr.GetResponse(); Stream ss = hwp.GetResponseStream(); byte[] buffer = new byte[10240]; if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } FileStream fs = new FileStream( string.Format(filepath + @"\" + filename), FileMode.Create); try { int i; while ((i = ss.Read(buffer, 0, buffer.Length)) >0) { fs.Write(buffer, 0, i); } fs.Close(); ss.Close(); } catch (Exception) { throw; } }

 

你可能感兴趣的:(C#实现在winfrom程序中下载文件)