首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>asp.net>资讯:ASP.NET生成静态HTML源码示例(C#)

ASP.NET生成静态HTML源码示例(C#)

www.jz123.cn  2012-12-21   来源:   站长家园    责任编辑(袁袁)    我要投递新闻

tohtml.cs 【生成静态页面核心类】:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
namespace stroper
{
    public class tohtml
    {
        public tohtml()
        {
       
        }
        /// <summary>
        /// 生成静态网页
        /// </summary>
        /// <param name="template_url">模板相对路径</param>
        /// <param name="Come_url">生成静态页面的相对路径</param>
        /// <param name="encode">页面编码</param>
        /// <param name="tags">静态模板标签</param>
        /// <param name="chg_tags">替换标签的内容</param>
        public string chg_html(string template_url,string Come_url,string encode,string[] tags,string[] chg_tags)
        {
            //初始化html字节流
            string init_stream = readsr(System.Web.HttpContext.Current.Server.MapPath(template_url), encode);
            //生成文件名
            string fileName=get_filename();
            //完成html字节流
            string ok_streamstr = chg_tag(tags, chg_tags, init_stream);
            //生成存放html的文件夹
            fileoper files = new fileoper();
            files.CreateFolder(Come_url);
            //生成静态页面
            bool okout = outhtml(System.Web.HttpContext.Current.Server.MapPath(Come_url)+"\\" + fileName, encode, ok_streamstr);
            //判断生成成功与失败
            if (okout == true)
            {
                return System.Web.HttpContext.Current.Server.MapPath(Come_url) + "\\" + fileName;
            }
            else
            {
                return "生成失败";
            }
        }
        /// <summary>
        /// 读取文件字节流
        /// </summary>
        /// <param name="template_url">模板相对路径</param>
        /// <param name="encode">页面编码</param>
        /// <returns>template静态模板的html字符串</returns>
        private string readsr(string template_url, string encode)
        {
            Encoding code = Encoding.GetEncoding(encode);
            StreamReader sr = null;
            string str = null;
            //读取
            try
            {
                sr = new StreamReader(template_url, code);
                str = sr.ReadToEnd();
                return str;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sr.Close();
            }
           
        }
        /// <summary>
        /// 生成静态页面的文件名
        /// </summary>
        /// <returns></returns>
        private string get_filename()
        {
            //Random ra = new Random(10);
            //string rnd=ra.Next(1, 999).ToString();
            long tick = DateTime.Now.Ticks;
            Random ra = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            string rnd = ra.Next(1, 999).ToString();
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +"-"+ rnd + ".htm";
            return fileName;
        }
        /// <summary>
        /// 模板的$标志替换
        /// </summary>
        /// <param name="tags">静态模板标签</param>
        /// <param name="chg_tags">替换标签的内容</param>
        /// <param name="streamstr">template静态模板的html字符串</param>
        /// <returns></returns>
        private string chg_tag(string[] tags,string[] chg_tags,string streamstr)
        {
            //str = str.Replace("$title$", txtTitle.Text);//替换Title
            //str = str.Replace("$content$", txtContent.Text);//替换content
            int i=0;
            for (i = 0; i <= tags.Length-1; i++)
            {
                streamstr = streamstr.Replace(tags, chg_tags[i]);
            }
            return streamstr;
        }
        /// <summary>
        /// 生成静态页面
        /// </summary>
        /// <param name="template_url">模板物理地址</param>
        /// <param name="Come_url">生成静态页面的相对路径</param>
        /// <param name="encode">页面编码</param>
        /// <param name="outhtmlstr">输出的html字符串</param>
        /// <returns></returns>
        private bool outhtml(string Come_url, string encode, string outhtmlstr)
        {
            StreamWriter sw = null;
            Encoding code = Encoding.GetEncoding(encode);
           
            try
            {
                sw = new StreamWriter(Come_url, false, code);
                sw.Write(outhtmlstr);
                sw.Flush();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
               
            }
        }
    }
}

fileoper.cs [文件处理类]:
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace stroper
{
    public class fileoper
    {
        /**/
        ///
        /// 在根目录下创建文件夹
        ///
        /// 要创建的文件路径
        public void CreateFolder(string FolderPathName)
        {
            if (FolderPathName.Trim().Length > 0)
            {
                try
                {
                    string CreatePath = System.Web.HttpContext.Current.Server.MapPath
                    (FolderPathName).ToString();
                    if (!Directory.Exists(CreatePath))
                    {
                        Directory.CreateDirectory(CreatePath);
                    }
                }
                catch
                {
                    throw;
                }
            }
        }
        /**/
        ///
        /// 删除一个文件夹下面的字文件夹和文件
        ///
        ///
        public void DeleteChildFolder(string FolderPathName)
        {
            if (FolderPathName.Trim().Length > 0)
            {
                try
                {
                    string CreatePath = System.Web.HttpContext.Current.Server.MapPath
                    (FolderPathName).ToString();
                    if (Directory.Exists(CreatePath))
                    {
                        Directory.Delete(CreatePath, true);
                    }
                }
                catch
                {
                    throw;
                }
            }
        }
        /**/
        ///
        /// 删除一个文件
        ///
        ///
        public void DeleteFile(string FilePathName)
        {
            try
            {
                FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath
                (FilePathName).ToString());
                DeleFile.Delete();
            }
            catch
            {
            }
        }
        public void CreateFile(string FilePathName)
        {
            try
            {
                //创建文件夹
                string[] strPath = FilePathName.Split('/');
                CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length - 1].ToString(), "")); //创建文件
                FileInfo CreateFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString()); //创建文件
                if (!CreateFile.Exists)
                {
                    FileStream FS = CreateFile.Create();
                    FS.Close();
                }
            }
            catch
            {
            }
        }
        /**/
        ///
        /// 删除整个文件夹及其字文件夹和文件
        ///
        ///
        public void DeleParentFolder(string FolderPathName)
        {
            try
            {
                DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath
                (FolderPathName).ToString());
                if (DelFolder.Exists)
                {
                    DelFolder.Delete();
                }
            }
            catch
            {
            }
        }
        /**/
        ///
        /// 在文件里追加内容
        ///
        ///
        public void ReWriteReadinnerText(string FilePathName, string WriteWord)
        {
            try
            {
                //建立文件夹和文件
                //CreateFolder(FilePathName);
                CreateFile(FilePathName);
                //得到原来文件的内容
                FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath
                (FilePathName).ToString(), FileMode.Open, FileAccess.ReadWrite);
                StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
                string OldString = FileReadWord.ReadToEnd().ToString();
                OldString = OldString + WriteWord;
                //把新的内容重新写入
                StreamWriter FileWrite = new StreamWriter(FileRead, System.Text.Encoding.Default);
                FileWrite.Write(WriteWord);
                //关闭
                FileWrite.Close();
                FileReadWord.Close();
                FileRead.Close();
            }
            catch
            {
                throw;
            }
        }
        /**/
        ///
        /// 在文件里追加内容
        ///
        ///
        public string ReaderFileData(string FilePathName)
        {
            try
            {
                FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath
                (FilePathName).ToString(), FileMode.Open, FileAccess.Read);
                StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
                string TxtString = FileReadWord.ReadToEnd().ToString();
                //关闭
                FileReadWord.Close();
                FileRead.Close();
                return TxtString;
            }
            catch
            {
                throw;
            }
        }
        /**/
        ///
        /// 读取文件夹的文件
        ///
        ///
        ///
        public DirectoryInfo checkValidSessionPath(string FilePathName)
        {
            try
            {
                DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath
                (FilePathName));
                return MainDir;
            }
            catch
            {
                throw;
            }
        }
    }
}


上一篇:.net 从后台得到htmlinput的值的2种方式 下一篇:最简单的ASP.NET数据库操作示例源代码

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图