代码改变世界

根据原文件生成指定的静态文件

2010-07-21 15:58  Tracy.  阅读(249)  评论(0)    收藏  举报

public class CreateHtmlHelper
    {
       
public CreateHtmlHelper()
        { }

       
/// <summary>
       
/// 根据原文件生成指定的静态文件
       
/// </summary>
       
/// <param name="_SourceFileName">原文件【虚拟路径】,如aspx</param>
       
/// <param name="_TargetFileName">目标静态文件【虚拟路径】,如html</param>
       
/// <param name="_Ed"></param>
       
/// <returns></returns>
        public static bool CreateHtml(string _SourceFileName,string _TargetFileName,Encoding _Ed)
        {
           
return WriteFile(HttpContext.Current.Server.MapPath(_TargetFileName), RenderPage(_SourceFileName), _Ed);
        }

       
#region 辅助方法

       
private static string RenderPage(string LocationPage)
        {
            StringBuilder v_Result
= new StringBuilder();
            HttpContext.Current.Server.Execute(LocationPage,
new HtmlTextWriter(new StringWriter(v_Result)));
           
return v_Result.ToString();
        }

       
private static bool WriteFile(string _FilePath, string _FileValue, Encoding _Encoding)
        {
           
try
            {
                StreamWriter sw
= new StreamWriter(_FilePath, false, _Encoding);
                sw.Write(_FileValue);
                sw.Close();
            }
           
catch
            {
               
return false;
            }
           
return true;
        }

       
#endregion
    }

这样调用:
CreateHtmlHelper.CreateHtml("快照网页", "网页名称", System.Text.Encoding.UTF8);