/// <summary>
/// 网页下载文件
/// </summary>
/// <param name="DownloadPath">目标地址</param>
/// <param name="FullFilePath">原地址</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool DownLoadSoft(string DownloadPath, string FullFilePath, string FileName)
{bool flag = false;
try
{
if (!Directory.Exists(DownloadPath))
{
Directory.CreateDirectory(DownloadPath);
}
using (FileStream fs = new FileStream(DownloadPath + "/" + FileName, FileMode.Create))
{
//创建请求
WebRequest request = WebRequest.Create(FullFilePath + FileName);
//接收响应
WebResponse response = request.GetResponse();
//输出流
Stream responseStream = response.GetResponseStream();
byte[] bufferBytes = new byte[10000];//缓冲字节数组
int bytesRead = -1;
while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
{
fs.Write(bufferBytes, 0, bytesRead);
}
if (fs.Length > 0)
{
flag = true;
}
//关闭写入
fs.Flush();
fs.Close();
}
}
catch (Exception exp)
{
//返回错误消息
}
return flag;
}
本文介绍了一种通过使用C#编程语言实现网页文件下载到指定路径的功能,包括创建目标目录、请求网页资源并将其保存至本地的过程。该实现包含了异常处理机制,确保在文件下载过程中遇到错误时能够优雅地捕获并报告。
2652

被折叠的 条评论
为什么被折叠?



