在asp.net中生成html文件代码
在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;
}