web

 
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 mshtml;

namespace WebBrowser_Download
{
    public partial class Form1 : Form
    {
        WebBrowser webUrl = new WebBrowser();
        public Form1()
        {
            InitializeComponent();
            this.btnAuto.Click += new EventHandler(btn_Click);
            this.btnManual.Click += new EventHandler(btn_Click);
            DownloadLink.Text = "www.yahoo.com.tw";
            this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);
            webUrl.ScriptErrorsSuppressed = true;
        }

        public void loading()
        {
            while (!(webUrl.ReadyState == WebBrowserReadyState.Complete))
            {
                //在網頁讀取的時候可以繼續其他事件
                Application.DoEvents();
            }
        }

        public void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //點選ListBox選項下載檔案
            webUrl.Navigate(listBox1.SelectedItem.ToString());
        }

        public void btn_Click(object sender, EventArgs e)
        {
            //連結到網站
            webUrl.Navigate(DownloadLink.Text);
            //等待網頁下載完成
            loading();
            //使用HTMLDocument語法
            HTMLDocument doc = webUrl.Document.Body.Document.DomDocument as HTMLDocument;
            //計算檔案次數
            int CountA = 0;
            WebClient wc = new WebClient();
            //暫存Url
            string strUrl = "";
            //儲存路徑
            string receivePath = @"C:\Users\jwwu\Desktop\WebBrowser_Download\DownLoad\";
            progressBar1.Minimum = 0;
          
            //自動下載btn
            if (sender.Equals(btnAuto))
            {
                //逐筆抓出有".doc"的網址
                foreach (IHTMLElement doclink in doc.links)
                {
                    strUrl = doclink.toString();
                    //Url文字包含".doc"
                    if (strUrl.Contains(".doc"))
                    {
                        CountA++;
                        progressBar1.Maximum=CountA;
                        progressBar1.Value += 1;
                        wc.DownloadFile(strUrl, receivePath + System.IO.Path.GetFileName(strUrl));
                    }
                }
                if (CountA == 0)
                {
                    MessageBox.Show("沒有word檔案可供下載", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("總共" + CountA + "筆", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (sender.Equals(btnManual))//手動下載
            {
                foreach (IHTMLElement doclink in doc.links)
                {
                    strUrl = doclink.toString();

                    if (strUrl.Contains(".doc"))
                    {
                        CountA++;
                        listBox1.Items.Add(strUrl);
                    }
                }
                if (CountA == 0)
                {
                    MessageBox.Show("沒有word檔案可供下載", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 熊仔 的頭像
    熊仔

    熊仔的部落格

    熊仔 發表在 痞客邦 留言(0) 人氣()