using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.IO;
using System.Text;
namespace NewsAdd
{
/// <summary>
/// 秦迷修订版本
/// 除了添加分页之外,还写了更新,更新之前删除所有静态分页的页面
/// 将 #删除旧文件,updatenewsSql# 注释掉 以及addnewsSql取消注释就是添加
/// </summary>
public partial class QinMiNewsAdd : System.Web.UI.Page
{
PubData pubdata = new PubData();
SqlConnection myConnection;
public void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
myConnection = new SqlConnection(pubdata.myconnectString);
myConnection.Open();
string strDate = DateTime.Now.ToString("yyMMdd") + "\\" + DateTime.Now.ToString("yyyymmddhhmmss");
string strFileName = strDate + ".html";
string strTitle = Request.Form["Title"].ToString();
string strContent = Request.Form["Content"].ToString();
string[] content = strContent.Split(‘|‘);//用 "|" 进行内容分页
int upbound = content.Length;
//string addnewsSql = "Insert Into News(Title,Content,FilePath)Values(‘" + strTitle + "‘,‘" + strContent + "‘,‘" + strFileName + "‘)";
//SqlCommand myCommand = new SqlCommand(addnewsSql, myConnection);
#region 删除旧文件
string OldContent = "";
string OldFilePath = "";
string newsSql = "Select Content,FilePath From News Where NewsId=50";
SqlCommand cmd = new SqlCommand(newsSql, myConnection);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read() != null)
{
OldContent = sdr["Content"].ToString();
OldFilePath = sdr["FilePath"].ToString();
}
sdr.Close();
string[] OldContentArr = OldContent.Split(‘|‘);
string OldFilePathPrefix = OldFilePath.Substring(0, OldFilePath.IndexOf(‘.‘));
DelFiles(OldFilePath);//删除根文件
for (int i = 0; i < OldContentArr.Length; i++)
{
DelFiles(OldFilePathPrefix + "_" + i + ".html");//循环删除分页文件
}
#endregion
string updatenewsSql = "Update News Set Title=‘" + strTitle + "‘,Content=‘" + strContent + "‘,FilePath=‘" + strFileName + "‘ Where NewsId=50";
SqlCommand myCommand = new SqlCommand(updatenewsSql, myConnection);
myCommand.ExecuteNonQuery();
Message.Text = "操作成功!";
//创建日期文件夹
string dir = Server.MapPath("NewsFiles/" + DateTime.Now.ToString("yyMMdd"));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
for (int i = 0; i < content.Length; i++)
{
StringBuilder strhtml = new StringBuilder();
//创建StreamReader对象
using (StreamReader sr = new StreamReader(Server.MapPath("template.htm"), Encoding.GetEncoding("gb2312")))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
//注:upUrl(上一页),Number(页码分页),downUrl(下一页) 这三个是用来替换的
string strTable = " <table> <tr> <td>upUrl</td> <td>Number</td> <td>downUrl</td> </tr> </table>";
string FilePath = "";
strhtml = strhtml.Replace("Title", strTitle);
strhtml = strhtml.Replace("Content", content[i]);
//数字分页
string strNumber = "";
for (int m = 1; m <= upbound; m++)
{
if (m == 1)//第一页:20070524.html而不是20070524_1.html
strNumber = strNumber + " [" + "<a href=" + "../" + strDate + ".html" + ">" + m + "</a>" + "] ";
else
{
int n = m - 1;//第三页:20070524_2.html...
strNumber = strNumber + " [" + "<a href=" + "../" + strDate + "_" + n + ".html" + ">" + m + "</a>" + "] ";
}
}
if (upbound == 0)//如果没有分页,就直接按日期时间保存
{
FilePath = Server.MapPath("NewsFiles") + "//" + strDate + ".html";
strhtml = strhtml.Replace("Pager", "");
}
else//否则按20070524.html、20070524_1.html 这种效果保存
{
if (i == 0)
FilePath = Server.MapPath("NewsFiles") + "//" + strDate + ".html";
else
FilePath = Server.MapPath("NewsFiles") + "//" + strDate + "_" + i + ".html";
if (i == 0)//第一页不显示上一页
strTable = strTable.Replace("upUrl", "");
if (i <= 1)//上一页分页
strTable = strTable.Replace("upUrl", "<a href=" + "../" + strDate + ".html" + ">上一页 </a>");
else
{
int p = i - 1;
strTable = strTable.Replace("upUrl", "<a href=" + "../" + strDate + "_" + p + ".html" + ">上一页 </a>");
}
if (upbound == 1)//如果只有一页,则不显示页码
strTable = strTable.Replace("Number", "");
else
strTable = strTable.Replace("Number", strNumber);//页码替换
if (i == upbound - 1)//最后一页不显示下一页
strTable = strTable.Replace("downUrl", "");
if (i != upbound - 1)//下一页分页
{
int q = i + 1;
strTable = strTable.Replace("downUrl", "<a href=" + "../" + strDate + "_" + q + ".html" + ">下一页 </a>");
}
else
{
int j = upbound - 1;
strTable = strTable.Replace("downUrl", "<a href=" + "../" + strDate + "_" + j + ".html" + ">下一页 </a>");
}
strhtml = strhtml.Replace("Pager", strTable);
}
//创建文件信息对象
FileInfo finfo = new FileInfo(FilePath);
//以打开或者写入的形式创建文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
}
}
catch (Exception err)
{
Response.Write(err.ToString());
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="_FileName">文件路径和文件名[包括扩展名]</param>
protected void DelFiles(string _FileName)
{
if (File.Exists(Server.MapPath("NewsFiles/" + _FileName)))
{
File.Delete(Server.MapPath("NewsFiles/" + _FileName));
}
}
}
}