>  기사  >  백엔드 개발  >  C#에서는 WebClient를 사용하여 파일 코드 세부 정보를 다운로드하는 두 가지 방법을 구현합니다.

C#에서는 WebClient를 사용하여 파일 코드 세부 정보를 다운로드하는 두 가지 방법을 구현합니다.

黄舟
黄舟원래의
2017-03-07 11:41:062807검색

이 글에서는 WebClient를 사용하여 C#에서 파일을 다운로드하는 두 가지 방법을 주로 소개하며, 도움이 필요한 친구들이 참고할 수 있는 유용한 내용입니다.

최근 WebClient를 사용하여 파일을 다운로드하는 두 가지 방법을 정리하고 추후 문의하도록 남겨두었습니다.

첫 번째 유형

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

string receivePath=@"C:\";

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

도 괜찮습니다.

두 번째 유형

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

위는 WebClient를 사용하여 파일을 다운로드하는 두 가지 방법을 구현한 C#의 내용입니다. 코드에 대한 자세한 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.