如题
我现在用网上常见的一个方法:如下
string path = HttpContext.Current.Server.MapPath("../html/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("../html/Temp.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename =path+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle
str = str.Replace("title", strText);
str = str.Replace("content", strContent);
str = str.Replace("author", strAuthor);
// 写文件
try
{
sw = new StreamWriter(htmlfilename, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
不知道是什么原因,执行操作提示成功,就是没有看到生成后的htm文件
这个方法好像也不能创建文件夹,我在网上看到很多人说用FSO 但是我不懂什么意思 到底要怎么弄才可以实现 自动创建如:20110822/XXXX.htm这种样子的
请各位哥哥姐姐帮忙解决下,
我现在用网上常见的一个方法:如下
string path = HttpContext.Current.Server.MapPath("../html/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("../html/Temp.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename =path+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle
str = str.Replace("title", strText);
str = str.Replace("content", strContent);
str = str.Replace("author", strAuthor);
// 写文件
try
{
sw = new StreamWriter(htmlfilename, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
不知道是什么原因,执行操作提示成功,就是没有看到生成后的htm文件
这个方法好像也不能创建文件夹,我在网上看到很多人说用FSO 但是我不懂什么意思 到底要怎么弄才可以实现 自动创建如:20110822/XXXX.htm这种样子的
请各位哥哥姐姐帮忙解决下,
解决方案 »
- 得到一个国外的日历js插件,不会用啊,/* jQuery timepicker * replaces a single text input with
- Repeater求某一列合计数?
- 请给我一个完整的asp.net项目实例!
- 〔asp.net2+c#〕求带附件发送email的代码,使用fileupload控件
- GridView中数据导入到Excel中出现自动换行的问题。
- 为什么总提醒需要“Sample Currently Unavailable Online”
- 如何在TREE中INSERT节点,重新加载后位置仍然不变??
- 关于验证控件与return confirm的問題
- 我的验证设为"Forms"了,为什么访问网站时,还要求输入用户名和密码呢?
- 请问如何在.cs文件中如何引用用户控件
- “未预编译文件”,发布网站后,某些页面总是出现这个
- 在下急等:关于livevalidation_standalone.js验证框架的使用错误
if(!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
protected bool makehtmlpage()
{
StreamWriter sw = null;
try
{
string sFileName = Server.MapPath("~/html/2011/08/22/");
if (!Directory.Exists(sFileName))
{
Directory.CreateDirectory(sFileName);
} sw = new StreamWriter(sFileName + "1A2C5DF89DR48EH5.html", false, System.Text.Encoding.UTF8); StringWriter stringW = new StringWriter();
HttpContext.Current.Server.Execute("~/show.aspx?ID=152753682", stringW);
sw.Write(stringW.ToString());
sw.Close();
return true;
} catch (Exception ex)
{
//这里必须关闭,否则内存占用增长....
if (sw != null) sw.Close();
return false;
}
finally
{
//这里必须关闭,否则内存占用增长....
if(sw!=null)sw.Close();
}
}
不过还是感谢各位,现在Head部分有点问题 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<LS下周六比赛活动>LS下周六比赛活动</LS下周六比赛活动>
<style type="text/css">
<!--
body {
background-color: #FFFFFF;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
font-size:12px;
}
.tabtext {color: #FFFFFF}
-->
</style>
</head>
<body>
标题标签那里 直接显示的文本内容 怎么样才能变成<title></title>呢
这个关键字和描述怎么添加啊 我用HtmlMeat这个类试过 不行
str = str.Replace("titles", strText); //把原来这里的title改为titles就可以了
str = str.Replace("content", strContent);
str = str.Replace("author", strAuthor);
关键字和描述也是同理,替换模版类容是替换所有的,包括<title></title>标签和<meta name="keywords" content="cttkeywords"/>标签里面的内容,所以关键字和描述你可以把cttkeywords替换成你想要的内容就可以了