using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace makehtmlfile
{
/// <summary>
/// makefile2 的摘要说明。
/// </summary>
public class makefile2 : System.Web.UI.Page
{
public string strcon;
public OleDbConnection conn;
public string class1id;
public string class2id;
//***********************************
public string previd;
public string prevtitle;
public string nextid;
public string nexttitle;
//***********************************
public string newstitle;
public string newscontent;
public string newsdate;
public string newsip;
public string newsid;
//************************************
private void Page_Load(object sender, System.EventArgs e)
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
if(Request.Params["id"]!=null&&Request.Params["class1id"]!=null&&Request.Params["class2id"]!=null)
{
InitialPages();
}// 在此处放置用户代码以初始化页面
}
public void InitialPages()
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";
if(Request.Params["id"]!=null)
{
newsid = Request.Params["id"].ToString();
}
if(Request.Params["class1id"]!=null)
{
class1id = Request.Params["class1id"].ToString();
}
if(Request.Params["class2id"]!=null)
{
class2id = Request.Params["class2id"].ToString();
}
ReadDataBase(newsid,class2id);
MakeHtmlFile(newsid,class1id,class2id);
}
/// <summary>
/// 读写同一分类中,上一篇,和下一篇文章
/// </summary>
/// <param name="inputid"> 该文章ID</param>
/// <param name="class2id">该文章所属分类ID</param>
public void ReadPrevAndNext(string inputid,string class2id)
{
int id = int.Parse(inputid);
string strPrevSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id<"+id+" order by id desc";
string strNextSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id>"+id+" order by id asc";
OleDbDataReader datar = null;
OleDbConnection con = new OleDbConnection(strcon);
con.Open();
OleDbCommand newcommand = new OleDbCommand(strPrevSQL,con);
datar = newcommand.ExecuteReader();
while(datar.Read())
{
previd = datar["id"].ToString();
prevtitle = datar["newstitle"].ToString();
}
datar.Close();
newcommand.CommandText = strNextSQL ;
datar = newcommand.ExecuteReader();
while(datar.Read())
{
nextid = datar["id"].ToString();
nexttitle = datar["newstitle"].ToString();
}
con.Close();
}
/// <summary>
/// 将文章信息从库中读出,并将准备生成的HTML文件路径写入库中
/// </summary>
/// <param name="inputid"></param>
/// <param name="class2id"></param>
public void ReadDataBase(string inputid,string class2id)
{
string filename_w = MakeFileName(class1id,class2id,newsid)+".htm";
ReadPrevAndNext(inputid,class2id); //读取下一篇和上一篇的信息。
OleDbConnection mycon = new OleDbConnection(strcon); //打开数据库连接
mycon.Open();
int id = int.Parse(inputid);
string strSQL = "select * from news where id="+id;
OleDbDataReader dr = null;
OleDbCommand mycommand = new OleDbCommand(strSQL,mycon);
dr = mycommand.ExecuteReader();
while(dr.Read())
{
newstitle = dr["newstitle"].ToString();
newscontent = dr["newscontent"].ToString();
newsdate = dr["newsdate"].ToString();
newsip = dr["newsip"].ToString();
}
dr.Close();
mycommand.CommandText = "update news set url='"+ filename_w +"' where id="+int.Parse(inputid); //将生成的文件路径写入库中,以遍在生成分类页中方便使用
mycommand.ExecuteNonQuery();
mycon.Close();
}
/// <summary>
/// 生成目标目录和文件,主要用来生成不同分类的目录
/// </summary>
/// <param name="inputstr"></param>
/// <returns></returns>
public string MakeCatalogName(string class1,string class2) //生成目标目录文件
{
string namestr = "Article";
string rootstr = Server.MapPath(".").ToString();
string class1str = rootstr + "\\" + namestr + "_" + class1 + "\\";
string class2str = rootstr + "\\" + namestr + "_" + class1 + "\\" + namestr + "_" + class2 + "\\";
if(!Directory.Exists(class1str))
{
Directory.CreateDirectory(class1str);
}
if(!Directory.Exists(class2str))
{
Directory.CreateDirectory(class2str);
}
//创建目标文件夹
return class2str;
}
/// <summary>
/// 根据文章分类和ID生成文件名
/// </summary>
/// <param name="class1id"></param>
/// <param name="class2id"></param>
/// <param name="nid"></param>
/// <returns>返回文件名</returns>
public string MakeFileName(string class1,string class2,string id) //生成文件名,能够生成上下篇
{
string myclass2id = class2;
string myclass1id = class1;
string s = DateTime.Now.Year.ToString()
+DateTime.Now.Month.ToString()
+DateTime.Now.Day.ToString()
+"_"
+myclass1id
+"_"
+myclass2id //父类ID
+"_"
+id; //新闻ID
return s;
}
/// <summary>
/// 生成HTML文件
/// </summary>
/// <param name="nid">文章ID号</param>
public void MakeHtmlFile(string nid,string cla1id,string cla2id) // MakeHtmlFile(string nid,string cla1id,string cla2id,string filetemp) 用于区分不同的摸班
{
string file_path = Server.MapPath ("template/news_mb.htm");
string desfilename = MakeFileName(cla1id,cla2id,nid)+".htm";
string desfile = MakeCatalogName(cla1id,cla2id)+MakeFileName(cla1id,cla2id,nid)+".htm";
string prevurl = MakeFileName(cla1id,cla2id,previd)+".htm"; //根据分类和ID生成上下篇的文件连接名
string nexturl = MakeFileName(cla1id,cla2id,nextid)+".htm"; //下篇
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
StreamReader srd = null; //读
StreamWriter swr = null; //写
string strFile = null ; //字符串
try
{
srd = new StreamReader(file_path, code);
strFile = srd.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
srd.Close();
}
strFile = strFile.Replace("$title$",newstitle);
strFile = strFile.Replace("$content$",newscontent);
strFile = strFile.Replace("$date$",newsdate);
strFile = strFile.Replace("$ip$",newsip);
strFile = strFile.Replace("$prev$",prevtitle);
strFile = strFile.Replace("$next$",nexttitle);
strFile = strFile.Replace("$prevurl$",prevurl);
strFile = strFile.Replace("$nexturl$",nexturl);
try
{
swr = new StreamWriter(desfile,false,code);
swr.Write(strFile);
swr.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
swr.Close();
}
if(srd!=null)
{
srd.Close();
}
Response.Write(desfilename);
}
public void Open()
{
if(conn==null)
{
conn = new OleDbConnection(strcon);
conn.Open();
}
}
public void Close()
{
if(conn!=null)
{
conn.Close();
}
}
Web 窗体设计器生成的代码
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace makehtmlfile
{
/// <summary>
/// makefile2 的摘要说明。
/// </summary>
public class makefile2 : System.Web.UI.Page
{
public string strcon;
public OleDbConnection conn;
public string class1id;
public string class2id;
//***********************************
public string previd;
public string prevtitle;
public string nextid;
public string nexttitle;
//***********************************
public string newstitle;
public string newscontent;
public string newsdate;
public string newsip;
public string newsid;
//************************************
private void Page_Load(object sender, System.EventArgs e)
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
if(Request.Params["id"]!=null&&Request.Params["class1id"]!=null&&Request.Params["class2id"]!=null)
{
InitialPages();
}// 在此处放置用户代码以初始化页面
}
public void InitialPages()
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";
if(Request.Params["id"]!=null)
{
newsid = Request.Params["id"].ToString();
}
if(Request.Params["class1id"]!=null)
{
class1id = Request.Params["class1id"].ToString();
}
if(Request.Params["class2id"]!=null)
{
class2id = Request.Params["class2id"].ToString();
}
ReadDataBase(newsid,class2id);
MakeHtmlFile(newsid,class1id,class2id);
}
/// <summary>
/// 读写同一分类中,上一篇,和下一篇文章
/// </summary>
/// <param name="inputid"> 该文章ID</param>
/// <param name="class2id">该文章所属分类ID</param>
public void ReadPrevAndNext(string inputid,string class2id)
{
int id = int.Parse(inputid);
string strPrevSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id<"+id+" order by id desc";
string strNextSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id>"+id+" order by id asc";
OleDbDataReader datar = null;
OleDbConnection con = new OleDbConnection(strcon);
con.Open();
OleDbCommand newcommand = new OleDbCommand(strPrevSQL,con);
datar = newcommand.ExecuteReader();
while(datar.Read())
{
previd = datar["id"].ToString();
prevtitle = datar["newstitle"].ToString();
}
datar.Close();
newcommand.CommandText = strNextSQL ;
datar = newcommand.ExecuteReader();
while(datar.Read())
{
nextid = datar["id"].ToString();
nexttitle = datar["newstitle"].ToString();
}
con.Close();
}
/// <summary>
/// 将文章信息从库中读出,并将准备生成的HTML文件路径写入库中
/// </summary>
/// <param name="inputid"></param>
/// <param name="class2id"></param>
public void ReadDataBase(string inputid,string class2id)
{
string filename_w = MakeFileName(class1id,class2id,newsid)+".htm";
ReadPrevAndNext(inputid,class2id); //读取下一篇和上一篇的信息。
OleDbConnection mycon = new OleDbConnection(strcon); //打开数据库连接
mycon.Open();
int id = int.Parse(inputid);
string strSQL = "select * from news where id="+id;
OleDbDataReader dr = null;
OleDbCommand mycommand = new OleDbCommand(strSQL,mycon);
dr = mycommand.ExecuteReader();
while(dr.Read())
{
newstitle = dr["newstitle"].ToString();
newscontent = dr["newscontent"].ToString();
newsdate = dr["newsdate"].ToString();
newsip = dr["newsip"].ToString();
}
dr.Close();
mycommand.CommandText = "update news set url='"+ filename_w +"' where id="+int.Parse(inputid); //将生成的文件路径写入库中,以遍在生成分类页中方便使用
mycommand.ExecuteNonQuery();
mycon.Close();
}
/// <summary>
/// 生成目标目录和文件,主要用来生成不同分类的目录
/// </summary>
/// <param name="inputstr"></param>
/// <returns></returns>
public string MakeCatalogName(string class1,string class2) //生成目标目录文件
{
string namestr = "Article";
string rootstr = Server.MapPath(".").ToString();
string class1str = rootstr + "\\" + namestr + "_" + class1 + "\\";
string class2str = rootstr + "\\" + namestr + "_" + class1 + "\\" + namestr + "_" + class2 + "\\";
if(!Directory.Exists(class1str))
{
Directory.CreateDirectory(class1str);
}
if(!Directory.Exists(class2str))
{
Directory.CreateDirectory(class2str);
}
//创建目标文件夹
return class2str;
}
/// <summary>
/// 根据文章分类和ID生成文件名
/// </summary>
/// <param name="class1id"></param>
/// <param name="class2id"></param>
/// <param name="nid"></param>
/// <returns>返回文件名</returns>
public string MakeFileName(string class1,string class2,string id) //生成文件名,能够生成上下篇
{
string myclass2id = class2;
string myclass1id = class1;
string s = DateTime.Now.Year.ToString()
+DateTime.Now.Month.ToString()
+DateTime.Now.Day.ToString()
+"_"
+myclass1id
+"_"
+myclass2id //父类ID
+"_"
+id; //新闻ID
return s;
}
/// <summary>
/// 生成HTML文件
/// </summary>
/// <param name="nid">文章ID号</param>
public void MakeHtmlFile(string nid,string cla1id,string cla2id) // MakeHtmlFile(string nid,string cla1id,string cla2id,string filetemp) 用于区分不同的摸班
{
string file_path = Server.MapPath ("template/news_mb.htm");
string desfilename = MakeFileName(cla1id,cla2id,nid)+".htm";
string desfile = MakeCatalogName(cla1id,cla2id)+MakeFileName(cla1id,cla2id,nid)+".htm";
string prevurl = MakeFileName(cla1id,cla2id,previd)+".htm"; //根据分类和ID生成上下篇的文件连接名
string nexturl = MakeFileName(cla1id,cla2id,nextid)+".htm"; //下篇
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
StreamReader srd = null; //读
StreamWriter swr = null; //写
string strFile = null ; //字符串
try
{
srd = new StreamReader(file_path, code);
strFile = srd.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
srd.Close();
}
strFile = strFile.Replace("$title$",newstitle);
strFile = strFile.Replace("$content$",newscontent);
strFile = strFile.Replace("$date$",newsdate);
strFile = strFile.Replace("$ip$",newsip);
strFile = strFile.Replace("$prev$",prevtitle);
strFile = strFile.Replace("$next$",nexttitle);
strFile = strFile.Replace("$prevurl$",prevurl);
strFile = strFile.Replace("$nexturl$",nexturl);
try
{
swr = new StreamWriter(desfile,false,code);
swr.Write(strFile);
swr.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
swr.Close();
}
if(srd!=null)
{
srd.Close();
}
Response.Write(desfilename);
}
public void Open()
{
if(conn==null)
{
conn = new OleDbConnection(strcon);
conn.Open();
}
}
public void Close()
{
if(conn!=null)
{
conn.Close();
}
}
Web 窗体设计器生成的代码
}
}