try
{
HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL);
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
//判断是否有这个文件,如果有这个文件就删除
if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab")) == true)
{
Directory.Delete(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab"), true);
}
long totalBytes = myrp.ContentLength;
if (Prog != null)
{
Prog.Maximum = (int)totalBytes;
}
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new FileStream(Filename, FileMode.Append, FileAccess.Write);
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
Application.DoEvents();
so.Write(by, 0, osize);
Prog.Value = (int)totalDownloadedByte;
osize = st.Read(by, 0, (int)by.Length);
percent = (float)totalDownloadedByte / (float)totalBytes * 100;