首页 > 编程 > .NET > 正文

生成静态文件的新闻系统核心代码(.net C#)(二)

2024-07-10 13:00:07
字体:
来源:转载
供稿:网友
 

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();
   }
  }

  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.load += new system.eventhandler(this.page_load);
  }
  #endregion
 }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表