生成静态页面(未完)--本人将继续整理资料

本文介绍了一个使用C#创建HTML文件的过程。通过读取HTML模板文件,并利用StringBuilder对象进行字符串拼接,实现对HTML内容的修改与替换。最终,将修改后的HTML内容写入新的HTML文件,并设置超链接方便预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建HTML文件


 1/**//// <summary>
 2/// 创建HTML文件
 3/// </summary>
 4private void CreateHtmlFile()
 5{   
 6    /**////定义和html标记数目一致的数组
 7    string[] newContent = new string[5];
 8    StringBuilder strhtml = new StringBuilder();
 9    try
10    {
11        /**////创建StreamReader对象
12        using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "
//template.html"))
13        {
14            String oneline;
15           
16            /**////读取指定的HTML文件模板
17            while ((oneline = sr.ReadLine()) != null)
18            {
19                strhtml.Append(oneline);
20            }
21            sr.Close();
22        }
23    }
24    catch(Exception err)
25    {
26        /**////输出异常信息
27        Response.Write(err.ToString());
28    }
29    /**////为标记数组赋值
30    newContent[0] = txtTitle.Text;//标题
31    newContent[1] = "BackColor='#cccfff'";//背景色
32    newContent[2] = "#ff0000";//字体颜色
33    newContent[3] = "100px";//字体大小
34    newContent[4] = txtContent.Text;//主要内容
35
36    /**////根据上面新的内容生成html文件
37    try
38    {
39        /**////指定要生成的HTML文件
40        string fname = Server.MapPath("createHTML") +"//" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
41       
42        /**////替换html模版文件里的标记为新的内容
43        for(int i=0;i < 5;i++)
44        {
45            strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
46        }
47        /**////创建文件信息对象
48        FileInfo finfo = new FileInfo(fname);
49       
50        /**////以打开或者写入的形式创建文件流
51        using(FileStream fs = finfo.OpenWrite())
52        {
53            /**////根据上面创建的文件流创建写数据流
54            StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
55           
56            /**////把新的内容写到创建的HTML页面中
57            sw.WriteLine(strhtml);
58            sw.Flush();
59            sw.Close();
60        }
61       
62        /**////设置超级链接的属性
63        hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
64        hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
65    }
66    catch(Exception err)
67    {
68        Response.Write (err.ToString());
69    }
70}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值