共享:对于URL重写,批量生成访问目录的方法
对于URL重写后,必须创建目录和首页文件才可以能过目录访问,如访问Stock/2006/10/12批量创建能够更方便~~程序很简单,大家不要见笑!:
private void BtnCreateFold_Click(object sender, System.EventArgs e)
{
Common.ImgUpLoad iul=new ImgUpLoad();
int StartDate=Convert.ToInt32(date1.Text); //起始日期
int EndDate=Convert.ToInt32(date2.Text); //结束日期
string Temp;
string fold=FirstFold.Text; //起始目录
for(int i=StartDate;i<=EndDate;i++)
{
iul.CreateFileName(fold+"/"+i,"Default.aspx");
Lit1.Text+="正在生成目录:<font color='red'>"+fold+"/"+i+"</font><br>";
for(int j=1;j<=12;j++)
{
if(j<10)
{
Temp="0"+j.ToString();
}
else
{
Temp=j.ToString();
}
iul.CreateFileName(fold+"/"+i+"/"+Temp,"Default.aspx");
Lit1.Text+="正在生成目录:<font color='red'>"+fold+"/"+i+"/"+Temp+"</font><br>";
}
}
}
UpLoad.cs:
#region 生成URL重写的目录和首页文件
public void CreateFileName(string fold,string name)
{
CheckFilePath(fold);
if(!File.Exists(HttpContext.Current.Server.MapPath(fold+"/"+name)))
{
File.Create(HttpContext.Current.Server.MapPath(fold+"/"+name));
}
}
#endregion
//生成文件夹
public void CheckFilePath(string path)
{
if(!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
}
}
HTML代码:
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr>
<td height="25" bgcolor="seashell" class="Title">批量生成目录:</td>
</tr>
<tr>
<td height="50" bgcolor="seashell">td>
</tr>
<tr>
<td height="30" align="center" bgcolor="seashell"><FONT face="宋体">起始目录:
<asp:TextBox id="FirstFold" runat="server" Width="89px">/Stock</asp:TextBox>
从</FONT>
<asp:TextBox id="date1" runat="server" Width="60px"></asp:TextBox><FONT face="宋体">年----到
<asp:TextBox id="date2" runat="server" Width="60px"></asp:TextBox>年
<asp:Button id="BtnCreateFold" runat="server" Text="开始生成"></asp:Button></FONT></td>
</tr>
<tr>
<td height="50" bgcolor="seashell">生成过程:<br>
<asp:Literal id="Lit1" runat="server"></asp:Literal>
</td>
</tr>
</table>