zoukankan
html css js c++ java
ASP.NET 生成静态html页之扩展(按年月目录)
在他人的基础上更改的!
代码如下:
详细内容看代码
using
System;
using
System.Data;
using
System.Configuration;
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;
using
System.Text;
using
System.Data.SqlClient;
/**/
///
<summary>
///
CreateHtml 的摘要说明
///
</summary>
public
class
CreateHtml
{
public
CreateHtml()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
public
static
bool
WriteFile(
string
title,
string
nfrom,
string
publishtime,
string
publisher,
string
contont)
{
//
//
以下定义
//
获取当前Web目录
//
模版文件
//
格式化文件名
//
用于生成年份目录
//
用于生成月份目录
//
从temple.html读取得字符
string
path
=
HttpContext.Current.Server.MapPath(
"
.
"
)
+
"
/news/
"
;
string
temp
=
path
+
"
temple.html
"
;
string
filename
=
DateTime.Now.ToString(
"
yyyyMMddHHmmss
"
)
+
"
.html
"
;
string
YYYYpath
=
path
+
DateTime.Now.Year.ToString();
string
MMpath
=
YYYYpath
+
"
/
"
+
DateTime.Now.Month.ToString();
string
str
=
""
;
StreamReader sr
=
null
;
StreamWriter sw
=
null
;
Encoding code
=
Encoding.GetEncoding(
"
gb2312
"
);
try
{
//
read file
sr
=
new
StreamReader(temp, code);
str
=
sr.ReadToEnd();
}
catch
(Exception ex)
{
HttpContext.Current.Response.Write(
"
读取错误
"
+
ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sr.Close();
}
//
replace
str
=
str.Replace(
"
{$title}
"
, title);
str
=
str.Replace(
"
{$nfrom}
"
, nfrom);
str
=
str.Replace(
"
{$publishtime}
"
, publishtime);
str
=
str.Replace(
"
{$publisher}
"
, publisher);
str
=
str.Replace(
"
{$contont}
"
, contont);
//
write
try
{
//
if Directory not Exists else Create Directory;
if
(
!
Directory.Exists(YYYYpath))
{
Directory.CreateDirectory(YYYYpath);
if
(
!
Directory.Exists(MMpath))
{
Directory.CreateDirectory(MMpath);
}
}
sw
=
new
StreamWriter(MMpath
+
"
/
"
+
filename,
false
, code);
sw.Write(str);
sw.Flush();
}
catch
(Exception ex)
{
HttpContext.Current.Response.Write(
"
写入失败
"
+
ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
//
update myurl
StringBuilder url
=
new
StringBuilder();
url.Append(
"
news/
"
);
url.Append(DateTime.Now.Year.ToString());
url.Append(
"
/
"
);
url.Append(DateTime.Now.Month.ToString());
url.Append(
"
/
"
);
url.Append(filename);
SqlConnection conn
=
null
;
try
{
conn
=
Db.createConnection();
//
从Db的静态方法得到SqlConnection
conn.Open();
SqlCommand cmd
=
new
SqlCommand(
"
update news set myurl='
"
+
url
+
"
'
"
,conn);
if
(
1
==
cmd.ExecuteNonQuery())
{
HttpContext.Current.Response.Write(
"
成功插入url<br/>
"
);
}
}
catch
(Exception ex)
{
HttpContext.Current.Response.Write(
"
更新url错误
"
+
ex.Message);
HttpContext.Current.Response.End();
}
finally
{
conn.Close();
}
return
true
;
}
}
查看全文
相关阅读:
iOS 3DTouch 的开发套路大全
启动图后面 添加有倒计时的广告图
激光推送报错:APNs is not available,please check your provisioning profile and certification 和 设置别名问题 app not registed, give up set tag:
iOS 微信和支付宝关于回调处理
Android 开机自启动应用
android快捷键
如何让Eclipse的智能提示像VS一样霸气
Win7下Eclipse中文字体太小
Android的SharedPreferences实用技巧
Java基础知识总结
原文地址:https://www.cnblogs.com/kokoliu/p/554527.html
最新文章
周总结7
《梦段代码》阅读笔记02
周总结6
顶会热词统计
Android------------------RecyclerView学习
Android---------------Handler的学习
Android---------------解决bug的关键点
Android---------------ContentProvider的学习
Android------TabLayout的使用
Android-----application的学习
热门文章
Android---------------Activity的学习
Android--------------BroadcastReceiver的学习
Android------------------系统服务调用的学习
Android------------fragment数据传递
iOS 集成支付宝过程中 我遇到的一些坑,请大家注意啦(ALI69错误,ALI64错误)
iOS 制作表格 (数据源控制行,列数)
tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift
securetextentry 切换后有空格问题解决
安装CocoaPods遇到的问题 及其解决
iOS 常用正则表达式
Copyright © 2011-2022 走看看