「C#」カテゴリーアーカイブ

ネット上にある画像ファイルをダウンロードして保存するサンプル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string jpgFileName = @"C:\tmp\test2.jpg";
            WebClient client = new WebClient();
            byte[] data = client.DownloadData("http://hogehogehogehoge/hogefuga.jpg");
            File.WriteAllBytes(jpgFileName, data);
        }
    }

}

とりあえずメモメモ

WebページのHTMLを取得してファイルに書き出すサンプル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string outputHtml = @"C:\tmp\yahoo.html";
            StreamWriter writer = new StreamWriter(outputHtml);

            WebClient client = new WebClient();
            string str = client.DownloadString("http://yahoo.com");
            writer.Write(str);

        }
    }
}

上記のサンプルで、YahooのWebページをHTMLまるごと取得して、C:\tmp\yahoo.htmlに書き出すことができる。

とりあえず

WebClientを使ってファイルをダウンロードする

メモメモ

WebClientクラスを使ってファイルをダウンロードする

System.Net空間にあるWebClientクラスのDownloadFileメソッドを使えば指定URLのファイルを簡単にダウンロードすることができます。
DownloadFileメソッドは引数が二つあり、一つ目が欲しいファイルのアドレス、二つ目が保存先のパスとなっています。

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;

続きを読む WebClientを使ってファイルをダウンロードする