110,534
社区成员
发帖
与我相关
我的任务
分享
public bool SaveHtml(string url, string path)
{
try
{
EnsurePath(path); //这个函数就是确保 path 中的文件夹是存在的.
string html = getHtmlText(url);
//这里写文件
StreamWriter wr = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
wr.Write(html);
wr.Flush();
wr.Close();
return true;
}
catch (Exception ex)
{ //往系统的日志里写出错信息
LogHelper.WriteErrorMessage("出错:" + ex.Message + "\nStackTrace:" + ex.StackTrace + "\nSource:" + ex.Source + "\nURL:" + url + "\n存储路径:" + path);
return false;
}
}
/// <summary>
/// 确保路径里的文件夹是存在的,不存在,就创建
/// </summary>
/// <param name="Inpath"></param>
public void EnsurePath(string Inpath)
{
string[] pathSplit = Inpath.Split('\\');
string path = pathSplit[0] + "\\" + pathSplit[1];
for (int i = 2; i < pathSplit.Length - 1; i++)
{
path += "\\" + pathSplit[i];
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
}