moban.htm(模版页)
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" align="center" border="1">
<TR>
<TD height="20"><FONT face="宋体">Asp.net生成Html文件</FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体">HtmlTitle</FONT></TD>
</TR>
<TR>
<TD height="9"><FONT face="宋体">HtmlContent</FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体">HtmlCreateDate</FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体"> </FONT>
</TD>
</TR>
</TABLE>
.aspx文件
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
cellPadding="1" width="300" border="1">
<TR>
<TD></TD>
<TD>生成Html文件</TD>
</TR>
<TR>
<TD>标题:</TD>
<TD>
<asp:TextBox id="name" runat="server"></asp:TextBox></TD>
</TR>
<TR>
<TD>内容:</TD>
<TD>
<asp:TextBox id="content" runat="server" TextMode="MultiLine" Width="230px" Height="96px"></asp:TextBox></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:Button id="createButton" runat="server" Text="生 成"></asp:Button>
<asp:Button id="escButton" runat="server" Text="取 消"></asp:Button></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
.aspx.cs后台代码
引用命名空间
using System.IO;
using System.Text;
关键代码:
private void createButton_Click(object sender, System.EventArgs e)
{//生成Html页程式
string savePath=Server.MapPath("News/");
if (Directory.Exists(savePath)==false){
//目录不存在
Directory.CreateDirectory(savePath);
}
Encoding code=Encoding.GetEncoding("gb2312");
string temp=Server.MapPath("moban.htm");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr=new StreamReader(temp,code);
str=sr.ReadToEnd();
}
catch(Exception ex){
Response.Write(ex.ToString());
sr.Close();
Response.End();
}
//获取文件名
string fileName=DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
string temptitle=name.Text;
string tempcontent=content.Text;
//过滤恶意字符
temptitle=temptitle.Replace("'","〃");
temptitle=temptitle.Replace("<","《");
temptitle=temptitle.Replace(">","》");
tempcontent=tempcontent.Replace("'","〃");
tempcontent=tempcontent.Replace("<","《");
tempcontent=tempcontent.Replace(">","》");
//替换空格和回车
temptitle=temptitle.Replace(" "," ");
tempcontent=tempcontent.Replace(" "," ");
tempcontent=tempcontent.Replace(Convert.ToChar(13).ToString(),"<br>");
//tempcontent=tempcontent.Replace("/n","<br>");
//替换标题,内容,生成时间
str=str.Replace("HtmlTitle",temptitle.ToString());
str=str.Replace("HtmlContent",tempcontent.ToString());
str=str.Replace("HtmlCreateDate",DateTime.Now.ToString());
//执行写入文件
try
{
sw=new StreamWriter(savePath+fileName,false,code);
sw.Write(str);
sw.Flush();
Response.Write("<script>alert('写入文件成功!');location.href='index.aspx'</script>");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
Response.End();
}
finally{
sw.Close();
}
}
其他IO相关
StreamReader Sr=new StreamReader(Server.MapPath("moban.htm"),Encoding.GetEncoding("gb2312"));
string str=Sr.ReadToEnd();
//readText.Text=str.ToString();
//str.GetEnumerator();
readText.Text="";
readText.Text+="/n"+str.IndexOf("<!--HtmlTitleStart-->");//第一个匹配项的索引
readText.Text+="/n"+str.IndexOf("<!--HtmlTitleEnd-->");
//Response.Write(str.Insert(str.IndexOf("<!--HtmlTitleStart-->"),"123").ToString());//插入字符
//str.Length;//获取字符数
//Response.Write(str.Remove(str.IndexOf("<!--HtmlTitleStart-->"),15));//删除字符
//获取执行位置的字符
//Response.Write(str.Substring(str.IndexOf("<!--HtmlTitleStart-->"),str.IndexOf("<!--HtmlTitleEnd-->")-str.IndexOf("<!--HtmlTitleStart-->")));
972

被折叠的 条评论
为什么被折叠?



