Winform下载文件

        /// <summary>
/// 下载文件
/// </summary>
/// <param name="URL">下载文件地址</param>
/// <param name="Filename">下载后的存放地址</param>
/// <param name="Prog">用于显示的进度条</param>
public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
{
try
{
System.Net.HttpWebRequest Myrq
= (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp
= (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;

if (prog != null)
{
prog.Maximum
= (int)totalBytes;
}

System.IO.Stream st
= myrp.GetResponseStream();
System.IO.Stream so
= new System.IO.FileStream(filename, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte
= osize + totalDownloadedByte;
System.Windows.Forms.Application.DoEvents();
so.Write(by,
0, osize);

if (prog != null)
{
prog.Value
= (int)totalDownloadedByte;
}
osize
= st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
}
catch (System.Exception)
{
throw;
}
}

/// <summary>
/// 下载文件
/// </summary>
/// <param name="URL">下载文件地址</param>
/// <param name="Filename">下载后的存放地址</param>
public void DownloadFile(string URL, string filename)
{
DownloadFile(URL, filename,
null);
}
posted on 2008-09-05 17:35  迷你软件  阅读(5057)  评论(3编辑  收藏  举报

本网站绝大部分资源来源于Internet,本站所有作品版权归原创作者所有!!如有以下内容:章节错误、非法内容、作者署名出错、版权疑问、作品内容有违相关法律等请及时与我联系. 我将在第一时间做出响应!本站所有文章观点不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。