1.通过相对路径得到绝对路径:
HttpContext.Current.Server.MapPath(path);
2.生成静态页面
message = "";
//文件生成路径
string target = HttpContext.Current.Server.MapPath(path);
//文件内容和脚本内容
StringBuilder str = new StringBuilder();
StringBuilder script = new StringBuilder();
//单个标签内容和单个脚本内容
string label = "";
string tempScript = "";
//第一个标签开始和结束
int left = content.IndexOf("┣");
int right = content.IndexOf("┫");
//当标签存在,生成标签内容
while (left >= 0 && right >= 0)
{
//标签前面内容直接输出
str.Append(content.Substring(0, left));
//得到标签名称
label = content.Substring(left + 1, right - left - 1);
//获取标签真实内容,和所需要脚本
str.Append(getModuleByLabel(label, out tempScript));
//暂存所有需要脚本
script.Append(tempScript + "/r/n");
//模板中已生成的内容截取掉
content = content.Substring(right + 1);
//重新设置第一个标签的开始和结束
left = content.IndexOf("┣");
right = content.IndexOf("┫");
}
//找到body前的最后位置,安置脚本
left = content.IndexOf("</body>");
//
StringBuilder function = new StringBuilder();
if (!isTest)
{
//不是测试页面,需要自动生成首页的js函数
function.Append("function createPage(date)");
function.Append("{/r/n");
function.Append(" var now = new Date().getMinutes();/r/n");
function.Append(" if(Math.abs(now-date)>1)/r/n");
function.Append(" {/r/n");
function.Append(" AjaxSend(function(){},'/kmhome/manage/createKMHome.aspx?action=createHome','');/r/n");
function.Append(" }/r/n");
function.Append("}/r/n");
function.AppendFormat("createPage('{0}');/r/n", DateTime.Now.ToString("mm"));
}
if (left >= 0)
{
str.Append(content.Substring(0, left));
str.AppendFormat("/r/n<script type='text/javascript'>/r/n{0}/r/n</script>/r/n", function.ToString());
str.Append(content.Substring(left));
}
else
{
str.AppendFormat("/r/n<script type='text/javascript'>/r/n{0}/r/n</script>/r/n", function.ToString());
}
str.AppendFormat("<!--LastCreateTime:{0}-->", DateTime.Now);
string result = "";
if (!isTest)
{
string homePath = "/html/kmhome/";
//生成首页前先备份旧的index.html
if (!Directory.Exists(HttpContext.Current.Server.MapPath(homePath + "bak/")))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(homePath + "bak/"));
}
string backurl = HttpContext.Current.Server.MapPath(string.Format(homePath + "bak/index_bak_{0}.html", DateTime.Now.ToString("yyyy-MM-dd_HH_mm")));
if (File.Exists(backurl))
{
File.Delete(backurl);
}
if (File.Exists(target))
{
File.Move(target, backurl);
}
}
try
{
using (StreamWriter writer = new StreamWriter(target, false, Encoding.GetEncoding("utf-8")))
{
writer.Write(str.ToString());
writer.Close();
string key = "KmHomeModulse";
if (HttpContext.Current.Cache[key] != null)
{
HttpContext.Current.Cache.Remove(key);
}
message = "首页文件生成成功。";
}
}
catch (Exception ex)
{
message = ex.Message;
return false;
}
return true;
3.判断文件夹是否存在
if (!Directory.Exists(HttpContext.Current.Server.MapPath(homePath + "bak/")))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(homePath + "bak/"));
}
string backurl = HttpContext.Current.Server.MapPath(string.Format(homePath + "bak/index_bak_{0}.html", DateTime.Now.ToString("yyyy-MM-dd_HH_mm")));
if (File.Exists(backurl))
{
File.Delete(backurl);
}
if (File.Exists(target))
{
File.Move(target, backurl);
}