动态生成修改aspx文件
using System.IO;
using System.Text;
/// <summary>
/// SCPage 的摘要说明 修改 aspx 和aspx.cs文件
/// </summary>
public class SCPage
{
public SCPage() { }
System.Web.UI.Page p = new Page();
/// <summary>
/// 生成aspx文件
/// </summary>
/// <param name="content"></param>
public bool SCPages(string pageName, string content)
{
// AccessPublicClass apc = new AccessPublicClass();
string path = p.Server.MapPath("BaseReport.aspx");
string module = FileStreamReader(path);
module = module.Replace("content", content);
string paths = p.Server.MapPath("Template/" + pageName + ".aspx");
module = module.Replace("BaseReport.aspx.cs", pageName + ".aspx.cs");
module = module.Replace("BaseReport", pageName);
bool result = FileStreamWrite(paths, module);
return result;
}
/// <summary>
/// 生成cs文件
/// </summary>
/// <param name="content"></param>
public bool SCCs(string pageName,string sqlInsert)
{
// AccessPublicClass apc = new AccessPublicClass();
string path = p.Server.MapPath("BaseReport.aspx.cs");
string module = FileStreamReader(path);
module = module.Replace("sqlInsertReplace", sqlInsert);
string paths = p.Server.MapPath("Template/"+pageName + ".aspx.cs");
module = module.Replace("BaseReport", pageName);
bool result = FileStreamWrite(paths, module);
return result;
}
/// <summary>
/// 对文件进行流的操作
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public string FileStreamReader(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
//从流的当前位置到末尾读取流
string v = sr.ReadToEnd();
fs.Close();
sr.Close();
return v;
}
public bool FileStreamWrite(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
try
{
//将文件写入流中
sw.WriteLine(content);
return true;
}
catch
{
return false;
}
finally
{
sw.Flush();
fs.Flush();
fs.Close();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。