C#下载文件代码更新20070920

引用空间:

using System.IO;
using System.Text;
using System.Net;

按钮事件:

private void button5_Click(object sender, System.EventArgs e)
{
string DownloadUrl="http://update.cz88.net/soft/qqwry.rar";

string LocalPath=Application.StartupPath.ToString()+"//"+"qqwry"+DateTime.Now.ToShortDateString()+".rar";

if(downfile(DownloadUrl,LocalPath))
{
MessageBox.Show("下载完成");
}
else
{
MessageBox.Show("下载过程中出现错误:");
}

}

下载函数:
public bool downfile(string url,string LocalPath)
{
try
{
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();
statusBar1.Text = "开始下载文件...";
Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);
long length = wr.ContentLength;
int i = 0;
long j=0;
statusBar1.Text = "正在接收数据...";
byte[] buffer = new byte[1024];
while ((i = sIn.Read(buffer, 0, buffer.Length)) > 0)
{
j+=i;
fs.Write(buffer, 0,i);
statusBar1.Text="文件大小:"+length.ToString()+"字节 当前下载:"+j+"字节";

}

sIn.Close();
wr.Close();
statusBar1.Text = "文件下载完毕...文件大小"+fs.Length.ToString()+"字节";
fs.Close();
return true;
}
catch { return false; }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值