批量下载工具

一款模仿迅雷批量下载的小工具,提供同步和异步两种下载方式。

public partial class Form1 : Form
    {
        string URL = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void realShow()
        {
            if (weburl.Text.Contains("*"))
            {
                string url2 = "";
                int min=0;
                int max=0;
                try { min = Int32.Parse(zmin.Text); }
                catch { };
                try{max = Int32.Parse(zmax.Text);}catch { }
                int xing = weburl.Text.IndexOf('*');
                string wildcard = wc.Text;
                string url = weburl.Text.Substring(0, xing);
                try
                {
                    url2 = weburl.Text.Substring(xing + 1, weburl.TextLength - xing - 1);
                }
                catch
                { }
                showurl.Text = url + min.ToString("d" + wildcard) + url2 + "\r\n" + "\r\n" + "…………" + "\r\n" + "\r\n" + "…………" + "\r\n\r\n" + url + max.ToString("d" + wildcard) + url2;
            }
        }

        private void downPic(string url,string fileName)
        {
            
            string myStringWebResource = null;
            fileName += ".jpg";
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            // Concatenate the domain with the Web resource filename.
            myStringWebResource = url + fileName;

            // Download the Web resource and save it into the current filesystem folder.
            myWebClient.DownloadFile(myStringWebResource, fileName);
            
        }

        private void downPicAsync(string url, string fileName)
        {
            fileName += ".jpg";
            WebClient client = new WebClient();
            Uri uri = new Uri(url+fileName);

            // Specify that the DownloadFileCallback method gets called
            // when the download completes.
            //client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback2);
            // Specify a progress notification handler.
            //client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
            client.DownloadFileAsync(uri,fileName);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int min = int.Parse(zmin.Text);
            int max = int.Parse(zmax.Text);
            string wildchar = wc.Text;
            int xing = weburl.Text.IndexOf('*');
            string url = weburl.Text.Substring(0, xing);

            if (radioButton1.Checked == true && min<max)
            {
                for (int i = min; i < max+1; i++)
                {
                    downPic(url, i.ToString("d"+wildchar));
                    toolStripStatusLabel1.Text = "正在下载" + i.ToString();
                    Application.DoEvents();
                }
            }
            else if(radioButton2.Checked == true && min<max)
            {
                for (int i = 1; i < 30; i++)
                {
                    downPicAsync(url, i.ToString("d"+wildchar));
                    toolStripStatusLabel1.Text = "正在下载" + i.ToString();
                    Application.DoEvents();
                }
            }
            else 
            {

            }
            toolStripStatusLabel1.Text = "下载完成";

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            realShow();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "批量下载";
            URL = weburl.Text;
            radioButton1.Select();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            realShow();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string min = "1";
           // string m;
           
            label1.Text =string.Format("{0:D5}", 23);
        }

        private void wc_TextChanged(object sender, EventArgs e)
        {
            realShow();
        }

        private void zmax_TextChanged(object sender, EventArgs e)
        {
            realShow();
        }
    }

下载地址:http://dl.dbank.com/c0oi9ty5tx

posted @ 2011-06-16 18:34  geekzph  阅读(424)  评论(1编辑  收藏  举报