Home >Backend Development >C#.Net Tutorial >C# uses WebClient to implement two ways to download file code details

C# uses WebClient to implement two ways to download file code details

黄舟
黄舟Original
2017-03-07 11:41:062996browse

This article mainly introduces the two methods of using WebClient to download files in C#. It introduces the two methods in detail. It is of great practical value. Friends in need can refer to it.

Recently sorted out the two ways of downloading files using WebClient and left them for future inquiry.

The first one

string URLAddress = @"http://xiazai.jb51.net";

string receivePath=@"C:\";

client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));

is OK.

Second type

 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();

The above is the content of C# using WebClient to implement two ways to download file code details. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn