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
;
}
}
查看全文
相关阅读:
miniprogramer--ajax
miniprogram 获取两点之间的距离(经纬度)
express node-schedule
express generate xls
protected default
Java UUID
关于轮播图兼容的问题
关于git上的一些错误信息
cookie
关于ajax原理介绍
原文地址:https://www.cnblogs.com/kokoliu/p/554527.html
最新文章
HTTP状态码完整版
网页前端优化
HTTP协议
ASPX1
Jquery4
Jquery3
Jquery2
c#学习3,构造函数
c#学习2,面向对象引用
c#简单概念(面向对象,类)
热门文章
uicontrol
matlab设计GUI(例子2)
初次学习matlab设计图形用户界面(GUI)
kinect笔记1(2014.4.14)
laravel migration
php orient object progarmming
form 上传图片不刷新
miniprogarme-avatar clip
miniprograme-wsx
miniprogrammer--gain element width,height and use npm
Copyright © 2011-2022 走看看