在做一个自动生成静态页面的时候遇到生成文件和文件夹的问题。
现在总结下来,以备后插。以下是测试页面。
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;
public partial class Manager_Major_TestWjj : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void dddd_Click(object sender, EventArgs e)
...{
string path;
//得到虚拟目录的物理路径。
path = HttpContext.Current.Server.MapPath("../../Template/ddd/");
try
...{
if (System.IO.Directory.Exists(path))
...{
Response.Write("目录已经存在!");
}
else
...{
System.IO.Directory.CreateDirectory(path);
Response.Write("创建成功!!");
}
}
catch (System.Exception err)
...{
throw err;
}
Response.Write(path);
}
}
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;
public partial class Manager_Major_TestWjj : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void dddd_Click(object sender, EventArgs e)
...{
string path;
//得到虚拟目录的物理路径。
path = HttpContext.Current.Server.MapPath("../../Template/ddd/");
try
...{
if (System.IO.Directory.Exists(path))
...{
Response.Write("目录已经存在!");
}
else
...{
System.IO.Directory.CreateDirectory(path);
Response.Write("创建成功!!");
}
}
catch (System.Exception err)
...{
throw err;
}
Response.Write(path);
}
}
页面文件
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
TestWjj.aspx.cs
"
Inherits
=
"
Manager_Major_TestWjj
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:Button ID = " dddd " Text = " dddd " runat = " server " OnClick = " dddd_Click " />
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:Button ID = " dddd " Text = " dddd " runat = " server " OnClick = " dddd_Click " />
</ div >
</ form >
</ body >
</ html >
这个例子中,HttpContext.Current.Server.MapPath("../../Template/ddd/");要根据实际情况来改写。
通过测试,可以生成文件加,如果有的话,就不再create。
ok,就这吧。