您的位置:首页 > Web前端 > HTML

在asp.net中生成html文件代码

2008-11-23 09:33 253 查看
在asp.net中生成html文件代码如下:

public static bool CreatHtmlPage(string[] strNewsHtml, string[] strOldHtml, string strModeFilePath, string strPageFilePath)

{

bool Flage = false;

StreamReader ReaderFile = null;

StreamWriter WrirteFile = null;

//修改mode.htm到inc目录下

strModeFilePath = "../inc/" + strModeFilePath;

string FilePath = HttpContext.Current.Server.MapPath(strModeFilePath);

Encoding Code = Encoding.GetEncoding("gb2312");

string strFile = string.Empty;

try

{

ReaderFile = new StreamReader(FilePath, Code);

strFile = ReaderFile.ReadToEnd();

}

catch (Exception ex)

{

throw ex;

}

finally

{

ReaderFile.Close();

}

try

{

int intLengTh = strNewsHtml.Length;

for (int i = 0; i < intLengTh; i++)

{

strFile = strFile.Replace(strOldHtml[i], strNewsHtml[i]);

}

WrirteFile = new StreamWriter(HttpContext.Current.Server.MapPath(strPageFilePath), false, Code);

WrirteFile.Write(strFile);

Flage = true;

}

catch (Exception ex)

{

throw ex;

}

finally

{

WrirteFile.Flush();

WrirteFile.Close();

}

return Flage;

}

public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)

{

bool Flage = false;

StreamReader ReaderFile = null;

StreamWriter WrirteFile = null;

string FilePath = HttpContext.Current.Server.MapPath(strHtml);

Encoding Code = Encoding.GetEncoding("gb2312");

string strFile = string.Empty;

try

{

ReaderFile = new StreamReader(FilePath, Code);

strFile = ReaderFile.ReadToEnd();

}

catch (Exception ex)

{

throw ex;

}

finally

{

ReaderFile.Close();

}

try

{

int intLengTh = strNewsHtml.Length;

for (int i = 0; i < intLengTh; i++)

{

int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;

int intEnd = strFile.IndexOf(strEndHtml[i]);

string strOldHtml = strFile.Substring(intStart, intEnd - intStart);

strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);

}

WrirteFile = new StreamWriter(FilePath, false, Code);

WrirteFile.Write(strFile);

Flage = true;

}

catch (Exception ex)

{

throw ex;

}

finally

{

WrirteFile.Flush();

WrirteFile.Close();

}

return Flage;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: