.net 生成静态页

来源:互联网 发布:淘宝白菜网站 编辑:程序博客网 时间:2024/05/22 00:25

   /// <summary>
    /// 取得绝对路径
    /// </summary>
    /// <param name="strPath"></param>
    /// <returns></returns>
    public static string MapPath(string strPath)
    {
        if (HttpContext.Current != null)
        {
            return HttpContext.Current.Server.MapPath(strPath);
        }
        else //非web程序引用
        {
            strPath = strPath.Replace("/", "//");
            if (strPath.StartsWith("~"))
            {
                strPath = strPath.Remove(0, 1);
            }
            if (strPath.StartsWith("//"))
            {
                //strPath = strPath.Substring(strPath.IndexOf('//', 1)).TrimStart('//');
                strPath = strPath.TrimStart('//');
            }
            return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }
    /// <summary>
    /// 生成html页
    /// </summary>
    /// <param name="str"></param>
    /// <param name="sb"></param>
    public static void setHtml(string str, StringBuilder sb)
    {
        string filePath = MapPath(str);
        string fileDir = filePath.Substring(0, filePath.LastIndexOf("//"));
        if (!Directory.Exists(fileDir))
        {
            Directory.CreateDirectory(fileDir);
        }
        StreamWriter sw = null;
        Encoding code = Encoding.GetEncoding("gb2312");
        try
        {
            //if (File.Create()
            //{

            //}
            sw = new StreamWriter(filePath, false, code);
            sw.Write(sb);
            sw.Flush();
            sw.Close();
        }
        catch
        {

            JScript.Alertto3("生成静态页失败"); //提示信息并 后退一步
        }
    }
    /// <summary>
    /// 生成html页
    /// </summary>
    /// <param name="str"></param>
    /// <param name="s"></param>
    public static void setHtml(string str, string s)
    {
        string filePath = System.Web.HttpContext.Current.Server.MapPath(str);
        StreamWriter sw = null;
        Encoding code = Encoding.GetEncoding("gb2312");
        try
        {
            sw = new StreamWriter(filePath, false, code);
            sw.Write(s);
            sw.Flush();
            sw.Close();
        }
        catch
        {

            JScript.Alertto3("生成静态页失败");
        }
    }

原创粉丝点击