最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#利用WebClient实现两种方式下载文件
时间:2017-03-16 编辑:简简单单 来源:一聚教程网
最近整理了WebClient 两种方式下载文件 ,留作以后查询。
第一种
代码如下 | 复制代码 |
stringURLAddress =@"http://xiazai.jb51.net";
stringreceivePath=@"C:\";
client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress)); |
就OK了。
第二种
代码如下 | 复制代码 |
Stream str = client.OpenRead(URLAddress); StreamReader reader =newStreamReader(str); byte[] mbyte =newbyte[1000000]; intallmybyte = (int)mbyte.Length; intstartmbyte = 0;
while(allmybyte > 0) {
intm = str.Read(mbyte, startmbyte, allmybyte); if(m == 0) break;
startmbyte += m; allmybyte -= m; }
reader.Dispose(); str.Dispose();
stringpath = receivePath + System.IO.Path.GetFileName(URLAddress); FileStream fstr =newFileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, startmbyte); fstr.Flush(); fstr.Close(); |
-
下一个: C#中加载dll并调用其函数的实现方法
相关文章
- C#实现判断文件夹存在与否并创建文件夹的方法 03-27
- C#中WebClient实现文件下载实现方案 11-16
- c# 下载.xlsx文件出错解决方法 05-09
- C# FileSystemWatcher 文件监控对象使用 01-22
- C# 调用PB的DLL文件 10-11
- 用C#制作PDF文件全攻略 01-12