C#程序进行文件下载
											 程序代码:
程序代码:/******其实我不懂这个文件下载要怎么做,这是我模仿别人的代码写出来的,求改错*****/
/******Textbox1:下载链接,Textbox2:文件名,Textbox3:文件保存目录和文件名**********/
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using using namespace FileDownload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox3.Text=@"C:\Documents and Settings\Administrator\桌面";
            this.label5.Text = "准备就绪";
        }
        private void btnOK_Click(object sender, EventArgs e)  //开始下载
        {
            this.btnOK.Enabled = false;
            this.label5.Text = "开始下载";
        
            WebClient client = new WebClient();
            string URL = this.textBox1.Text.Trim();
            int n = URL.LastIndexOf('/');
            string URLAddress = URL.Substring(0, n);
            string Dir = this.textBox3.Text.Trim();
            string Path = Dir + "\\" + this.textBox2.Text.Trim();
            if (Path.Contains(@"\\") == true)
            {
                Path.Replace(@"\\", @"\");
            }
            try
            {
                WebRequest myre = WebRequest.Create(URLAddress);  //为何程序每次运行到这里都出错了呢?
                try
                {
                    client.DownloadFile(URLAddress, this.textBox2.Text.Trim());
                    Stream str = client.OpenRead(URLAddress);
                    StreamReader sr = new StreamReader(str);
                    byte[] mbyte = new byte[100000000];  //这个大小是不是限制了可以下载最大文件的大小?
                    int allmybyte = (int)mbyte.Length;
                    int startbyte = 0;
                    while (allmybyte > 0)
                    {
                        int m = str.Read(mbyte, startbyte, allmybyte);
                        if (m == 0)
                            break;
                        startbyte += m;
                        allmybyte = m;
                    }
                    FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
                    fstr.Write(mbyte, 0, startbyte);
                    str.Close();
                    fstr.Close();
                    this.label5.Text = "下载完毕";
                    btnOK.Enabled = true;
                }
                catch (Exception o)
                {
                    MessageBox.Show("连接失败:"+o.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    btnOK.Enabled = true;
                    this.label5.Text = "连接失败";
                }
            }
            catch(Exception o)
            {
                MessageBox.Show(o.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnOK.Enabled = true;
                this.label5.Text = "下载失败";
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)  //下载链接更改
        {
            string s = this.textBox1.Text.Trim();  //下载链接
            if (s == string.Empty)
            {
                this.textBox2.Clear();
            }
            else
            {
                int n = s.LastIndexOf('/');
                string str = s.Substring(n + 1, s.Length - n - 1);
                this.textBox2.Text = str;  //根据下载链接判断文件名
            }
        }
    }
}[ 本帖最后由 qq1023569223 于 2011-6-6 00:24 编辑 ]

 
											







 
	    