首页 > 开发 > 综合 > 正文

用Visual C#实现文件下载

2024-07-21 02:29:30
字体:
来源:转载
供稿:网友

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.net;
using system.io;
using system.threading;
  
namespace mygetcar
{
 ///
 /// form1 的摘要说明。
 ///
 public class form1 : system.windows.forms.form
 {
  private system.windows.forms.label label1;
  private system.windows.forms.label label2;
  private system.windows.forms.textbox srcaddress;
  private system.windows.forms.textbox taraddress;
  private system.windows.forms.statusbar statusbar;
  private system.windows.forms.button start;
  
  private webclient client = new webclient();
  
   
  ///
  /// 必需的设计器变量。
  ///
  private system.componentmodel.container components = null;
  
  public form1()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();
  
   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }
  
  ///
  /// 清理所有正在使用的资源。
  ///
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }
  
  #region windows form designer generated code
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void initializecomponent()
  {
   this.label1 = new system.windows.forms.label();
   this.label2 = new system.windows.forms.label();
   this.srcaddress = new system.windows.forms.textbox();
   this.taraddress = new system.windows.forms.textbox();
   this.statusbar = new system.windows.forms.statusbar();
   this.start = new system.windows.forms.button();
   this.button1 = new system.windows.forms.button();
   this.suspendlayout();
   //
   // label1
   //
   this.label1.location = new system.drawing.point(8, 32);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(72, 23);
   this.label1.tabindex = 0;
   this.label1.text = "文件地址:";
   this.label1.textalign = system.drawing.contentalignment.middleright;
   //
   // label2
   //
   this.label2.location = new system.drawing.point(8, 72);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(72, 23);
   this.label2.tabindex = 1;
   this.label2.text = "另存到:";
   this.label2.textalign = system.drawing.contentalignment.middleright;
   //
   // srcaddress
   //
   this.srcaddress.location = new system.drawing.point(80, 32);
   this.srcaddress.name = "srcaddress";
   this.srcaddress.size = new system.drawing.size(216, 21);
   this.srcaddress.tabindex = 2;
   this.srcaddress.text = "";
   //
   // taraddress
   //
   this.taraddress.location = new system.drawing.point(80, 72);
   this.taraddress.name = "taraddress";
   this.taraddress.size = new system.drawing.size(216, 21);
   this.taraddress.tabindex = 3;
   this.taraddress.text = "";
   //
   // statusbar
   //
   this.statusbar.location = new system.drawing.point(0, 151);
   this.statusbar.name = "statusbar";
   this.statusbar.size = new system.drawing.size(312, 22);
   this.statusbar.tabindex = 4;
   //
   // start
   //
   this.start.flatstyle = system.windows.forms.flatstyle.flat;
   this.start.location = new system.drawing.point(216, 112);
   this.start.name = "start";
   this.start.size = new system.drawing.size(75, 24);
   this.start.tabindex = 5;
   this.start.text = "开始下载";
   this.start.click += new system.eventhandler(this.start_click);
   
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(312, 173);
   this.controls.add(this.button1);
   this.controls.add(this.start);
   this.controls.add(this.statusbar);
   this.controls.add(this.taraddress);
   this.controls.add(this.srcaddress);
   this.controls.add(this.label2);
   this.controls.add(this.label1);
   this.maximizebox = false;
   this.name = "form1";
   this.text = "文件下载器";
   this.resumelayout(false);

  }
  #endregion
  
  ///
  /// 应用程序的主入口点。
  ///
  [stathread]
  static void main()
  {
   application.run(new form1());
  }
  
  private void startdownload()
  {
   start.enabled = false;
   string url = srcaddress.text;
   int n = url.lastindexof("?");
   string urladdress = url;
   //string urladdress = url.substring(0,n);
   string filename = url.substring(n+1,url.length-n-1);
   string dir = taraddress.text;
   string path = dir+filename;
  
   try
   {
    webrequest myre=webrequest.create(urladdress);
   }
   catch(webexception exp)
   {
    messagebox.show(exp.message,"error");
   }
  
   try
   {
    statusbar.text = "开始下载文件...";
    client.downloadfile(urladdress,filename);
    stream str = client.openread(urladdress);
    //streamreader reader = new streamreader(str); ,这句没有什么必要!个人认为
    byte[] mbyte = new byte[100000];
    int allmybyte = (int)mbyte.length;
    int startmbyte = 0;
    statusbar.text = "正在接收数据...";
    //写入到byte数组中,起缓冲作用
    while(allmybyte>0)
    {
     int m = str.read(mbyte,startmbyte,allmybyte);
     if(m==0)
      break;
  
     startmbyte+=m;
     allmybyte-=m;
    }
  
    filestream fstr = new filestream(path,filemode.openorcreate,fileaccess.write);
    fstr.write(mbyte,0,startmbyte);
    str.close();
    fstr.close();
  
    statusbar.text = "下载完毕!";
   }
   catch(webexception exp)
   {
    messagebox.show(exp.message,"error");
    statusbar.text = "";
   }
  
   start.enabled = true;
  }
  
  
  private void start_click(object sender, system.eventargs e)
  {
   thread th = new thread(new threadstart(startdownload));
   th.start();
  }
 }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表