天天看點

WebClient下載下傳圖檔示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Faron_WinFormApp
{
    public partial class WebClient_DownPic : Form
    {
        private string picUrl;

        public WebClient_DownPic()
        {
            InitializeComponent();
        }

        private void DownLoadPic(string url)
        {
            string picName = url.Substring(url.LastIndexOf("/")+1);
            Uri uri = new Uri(url);

            WebClient wc = new WebClient();
            try
            {
                //Bitmap bmp = new Bitmap(wc.OpenRead(uri));
                wc.DownloadFile(uri, Directory.GetCurrentDirectory() + "/" + picName);
                MessageBox.Show("成功下載下傳圖檔" + picName);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (wc != null)
                    wc.Dispose();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            picUrl = this.textBox1.Text;
            if (picUrl == string.Empty)
                MessageBox.Show("請輸入圖檔位址");
            else
                DownLoadPic(picUrl);
        }
    }
}
           

繼續閱讀