C#winformファイルのダウンロード機能を実現

1222 ワード

1.まずフォームを新規作成し、Buttonを追加
2.Buttonのクリックイベントにコードを追加する:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            byte[] data = client.DownloadData("http://b.hiphotos.baidu.com/zhidao/pic/item/3c6d55fbb2fb4316641d646623a4462309f7d3af.jpg");// , , 
            // , 
            string path =Application.StartupPath;
            FileStream fs = new FileStream(path+"\\x.jpg", FileMode.Create);   
            // byte 
            fs.Write(data,0,data.Length);     
            fs.Close();
        }
    }
}

FileModeは、オペレーティングシステムがファイルを開く方法の列挙であり、6つのリロード方法があります.
Append,Create,CreateNew,Open,OpenOrCreate,Truncate