// 从服务器下载文件
// 成功返回 true,否则返回false
// using System.Net;
private bool DownloadFile(string url, string strfile)
{
HttpWebRequest hwr = null;
Stream ns = null;
FileStream fs = new FileStream(strfile, FileMode.Create);
int nReadSize = 0;
byte[] abytes = new byte[1025];
try
{
hwr = (HttpWebRequest)WebRequest.Create(url);
ns = hwr.GetResponse().GetResponseStream();
nReadSize = ns.Read(abytes, 0, 1024);
while (nReadSize > 0)
{
fs.Write(abytes, 0, nReadSize);
nReadSize = ns.Read(abytes, 0, 1024);
}
fs.Close();
ns.Close();
}
catch (Exception eer)
{
MessageBox.Show(eer.Message);
fs.Close();
File.Delete(strfile);
return false;
}
return true;
}
谢谢!我借用了这个程序。我的博客是http://www.div.cn,呵呵
ReplyDelete