ホームページ  >  記事  >  バックエンド開発  >  C# は WebClient を使用して、ファイル コードの詳細をダウンロードする 2 つの方法を実装します。

C# は WebClient を使用して、ファイル コードの詳細をダウンロードする 2 つの方法を実装します。

黄舟
黄舟オリジナル
2017-03-07 11:41:062806ブラウズ

この記事では主に C# で WebClient を使用してファイルをダウンロードする 2 つの方法を詳しく紹介します。必要な方は参考にしてください。

最近、WebClient を使用してファイルをダウンロードする 2 つの方法を整理し、将来の調査のために残しました。

最初の

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

string receivePath=@"C:\";

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

はOKです。

2 つ目

 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 を使用してファイル コードをダウンロードする 2 つの方法を実装する C# の内容です。関連コンテンツの詳細については、PHP 中国語 Web サイト (www.php.cn) を参照してください。 )!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。