天天看點

C# 下載下傳圖檔到本地

先建立一個類檔案,然後建立函數,調用它就可以下載下傳圖檔。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;


namespace Spider
{
    class DocumentWorker
    {
        /// <summary>
        /// Download a page
        /// </summary>
        /// <returns>The data downloaded from the page</returns>
     
        public void saveimage()
        {
            WebClient mywebclient = new WebClient();
            string url = "http://images.google.cn/intl/zh-CN_ALL/images/images_hp.gif";
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +  DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
            string filepath = @"F:\" + newfilename;
            try
            {
                mywebclient.DownloadFile(url, filepath);
                //filename = newfilename;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}