WebClient的使用

來源:互聯網
上載者:User

標籤:open   wait   save   false   name   硬碟   dispose   button   art   

1、下載網頁源碼:

   private void button1_Click(object sender, EventArgs e)        {            string url = textBox1.Text;            string toUrl = Application.StartupPath + "\\" + Path.GetFileName(url);            WebClient wc = new WebClient();           wc.DownloadDataAsync(new Uri(url));           wc.DownloadDataCompleted += wc_DownloadDataCompleted;        }        void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)        {            if (e.Error == null && e.Cancelled == false)                textBox2.Text = Encoding.UTF8.GetString(e.Result);            else                MessageBox.Show(e.Error.Message);        }

 2、直接下載檔案,比較簡單,但下載比較傷硬碟:

        private void button1_Click(object sender, EventArgs e)        {            string url = textBox1.Text;            string toUrl = Application.StartupPath + "\\" + Path.GetFileName(url);            WebClient wc = new WebClient();            wc.DownloadFileCompleted += wc_DownloadFileCompleted;            wc.DownloadFileAsync(new Uri(url), toUrl);        }        void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)        {            MessageBox.Show("下載完成");        }

3、利用緩衝下載檔案,可以更好的保護硬碟

private void button2_Click(object sender, EventArgs e)        {            WebClient wc = new WebClient();            wc.OpenReadAsync(new Uri(textBox1.Text));            wc.OpenReadCompleted += wc_OpenReadCompleted;        }        async void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)        {            if (e.Error == null)            {                await SaveFile(e.Result, Application.StartupPath + "\\" + Path.GetFileName(textBox1.Text));                MessageBox.Show("下載完成");            }            else            {                MessageBox.Show(e.Error.Message);            }        }        Task SaveFile(Stream stream, string savepath)        {            return Task.Run(() =>              {                  var read = stream;                  byte[] buf = new byte[8192];                  int res = 0;                  FileStream fs = File.Open(savepath, FileMode.OpenOrCreate);                  using (fs)                  {                      while ((res = read.Read(buf, 0, buf.Length)) > 0)                      {                          fs.Write(buf, 0, res);                          fs.Flush();                      }                  }                  read.Close();                  read.Dispose();              });        } 

4、上傳檔案

WebClient的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.