using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Management;
GetDownFiles(sUrl, ( "C:\DownFiles.Tmp"), LabDownMess, pBar2);
//下载文件
//=================================================================================
public static Boolean GetDownFiles(string url, string LocalPath,Label LabDownMess,ProgressBar pBar2)
{
try
{
//先删除临时下载文件
if (File.Exists(LocalPath))
{
File.Delete(LocalPath);
}
//下载
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();
Stream sIn = wr.GetResponseStream();
long length = wr.ContentLength;
pBar2.Minimum = 0;
pBar2.Maximum = (Int32)length;
pBar2.Value = 0;
byte[] mbyte = new byte[100000000];
int startmbyte = 0;
while (length > 0)
{
int m = sIn.Read(mbyte, startmbyte, (int)length);
if (m == 0)
break;
startmbyte += m;
length -= m;
pBar2.Value = (Int32)startmbyte;
LabDownMess.Text = "当前下载文件大小:" + (length + startmbyte).ToString() + "字节 当前下载大小:" + startmbyte.ToString() + "字节";
Application.DoEvents();
}
FileStream fstr = new FileStream(LocalPath, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
sIn.Close();
fstr.Close();
wr.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}