C#利用WebClient實現兩種方式下載文件
更新時間:2017年02月06日 14:29:25 作者:x4646
本篇文章主要介紹了C#利用WebClient 兩種方式下載文件,詳細的介紹了兩種方式,非常具有實用價值,需要的朋友可以參考下。
最近整理了WebClient 兩種方式下載文件 ,留作以后查詢。
第一種
string URLAddress = @"http://xiazai.jb51.net"; string receivePath=@"C:\"; client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
就OK了。
第二種
Stream str = client.OpenRead(URLAddress); StreamReader reader = new StreamReader(str); byte[] mbyte = new byte[1000000]; int allmybyte = (int)mbyte.Length; int startmbyte = 0; while (allmybyte > 0) { int m = str.Read(mbyte, startmbyte, allmybyte); if (m == 0) break; startmbyte += m; allmybyte -= m; } reader.Dispose(); str.Dispose(); string path = receivePath + System.IO.Path.GetFileName(URLAddress); FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, startmbyte); fstr.Flush(); fstr.Close();
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C# 使用Free Spire.Presentation 實現對PPT插入、編輯、刪除表格
小編發(fā)現使用.NET組件——Free Spire.Presentation,在C#中添加該產品DLL文件,可以簡單快速地實現對演示文稿的表格插入、編輯和刪除等操作,具體實現代碼大家參考下本文吧2017-09-09DevExpress實現禁用TreeListNode CheckBox的方法
這篇文章主要介紹了DevExpress實現禁用TreeListNode CheckBox的方法,在項目開發(fā)中有應用價值,需要的朋友可以參考下2014-08-08