首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

提供个C#实现文件下载代码


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  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.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.AddRange(new System.Windows.Forms.Control[] {
  this.Start,
  this.statusBar,
  this.tarAddress,
  this.srcAddress,
  this.label2,
  this.label1});
  this.MaximizeBox = false;[Page]
  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.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 = \"正在接收数据...\";
  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();
  }
  }
  }
  
  四.总结:
  
  以上我通过一个实例向大家展示了如何用Visual C#实现网络文件的下载,我们不难发现用Visual C#进行Internet通讯编程是非常方便的。在上面的程序中,我们仅仅用到了WebClient类的一些方法,而WebClient类不光提供了网络文件下载的方法,还提供了文件上传的方法,有兴趣的读者不妨一试――用之实现一个文件上传器。同时这个程序只是一个非常简单的例子,程序下载完一个网页后,它所获得的仅仅是主页面的内容,并不能获得其中的图片、CSS等文件,所以要做出一个比较好的文件下载器还需读者进一步改进之。

上一页  [1] [2] 

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航