近日在做一个有关读取文件路径的东东,但路径中的中文文件夹名不可以超过四个字,不然会报错,请版主帮忙看一下:
例如路径为:e:\abc\abc\新建文件夹,就会报错,但是e:\abc\abc\abcccccccccccc,再长也不报错。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace RS
{
/// <summary>
/// CommunityManager 的摘要说明。
/// </summary>
public class CommunityManager : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlTable Table1;
private string path="";
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
if(Request.QueryString["path"]==null)
{
this.Table1.Rows[1].Cells[0].InnerHtml="<p align=center><a href=CommunityManager.aspx?path="+ConfigurationSettings.AppSettings["interspace"].ToString()+"\\public class='navLink'><font color='#003366'>公共空间</font></a></p>";
this.Table1.Rows[2].Cells[0].InnerHtml="<p align=center><a href=CommunityManager.aspx?path="+ConfigurationSettings.AppSettings["interspace"].ToString()+"\\private class='navLink'><font color='#003366'>个人空间</font></a></p>";
this.Table1.Rows[1].Cells[1].InnerHtml="<p align=center><a href=addProto.aspx?path="+ConfigurationSettings.AppSettings["interspace"].ToString()+"\\public class='navLink'><font color='#003366'>添加文件夹</font></a>";
this.Table1.Rows[2].Cells[1].InnerHtml="<p align=center><a href=addProto.aspx?path="+ConfigurationSettings.AppSettings["interspace"].ToString()+"\\private class='navLink'><font color='#003366'>添加文件夹</font></a>";
string[] dir=Directory.GetDirectories(ConfigurationSettings.AppSettings["interspace"].ToString()+"\\public");
this.Table1.Rows[1].Cells[2].InnerHtml="<p align=center><font color='#003366' class='fon'>"+dir.Length+"文件夹</font></p>";
string[] files=Directory.GetFiles(ConfigurationSettings.AppSettings["interspace"].ToString()+"\\public");
this.Table1.Rows[1].Cells[3].InnerHtml="<p align=center><font color='#003366' class='fon'>"+files.Length+"文件</font></p>";
dir=Directory.GetDirectories(ConfigurationSettings.AppSettings["interspace"].ToString()+"\\private");
this.Table1.Rows[2].Cells[2].InnerHtml="<p align=center><font color='#003366' class='fon'>"+dir.Length+"文件夹</font></p>";
files=Directory.GetFiles(ConfigurationSettings.AppSettings["interspace"].ToString()+"\\private");
this.Table1.Rows[2].Cells[3].InnerHtml="<p align=center><font color='#003366' class='fon'>"+files.Length+"文件</font></p>";
}
else
{
path=Request.QueryString["path"].ToString();
DirectoryInfo di=new DirectoryInfo(path);
DirectoryInfo[] dir=di.GetDirectories();
FileInfo[] files=di.GetFiles();
if(dir.Length+files.Length>this.Table1.Rows.Count-1)
{
for(int i=0;i<dir.Length;i++)
{
HtmlTableRow r=new HtmlTableRow();
r.BgColor="#ffffff";
for(int j=0;j<4;j++)
{
HtmlTableCell cell=new HtmlTableCell();
cell.Align="center";
r.Cells.Add(cell);
}
this.Table1.Rows.Add(r);
}
for(int i=0;i<files.Length;i++)
{
HtmlTableRow rr=new HtmlTableRow();
rr.BgColor="#ffffff";
for(int j=0;j<4;j++)
{
HtmlTableCell cells=new HtmlTableCell();
cells.Align="center";
rr.Cells.Add(cells);
}
this.Table1.Rows.Add(rr);
}
}
int rowcount=1;
for(int i=0;i<dir.Length;i++)
{
this.Table1.Rows[rowcount].Cells[0].InnerHtml="<p align=center><a href=CommunityManager.aspx?path="+dir[i].FullName+" class='navLink'><font color='#003366'>"+dir[i].Name+"</font></a></p>";
this.Table1.Rows[rowcount].Cells[1].InnerHtml="<p align=center><a href=addProto.aspx?path="+dir[i].FullName+" class='navLink'><font color='#003366'>添加文件夹</font></a></p>";
string[] childdir=Directory.GetDirectories(dir[i].FullName);
this.Table1.Rows[rowcount].Cells[2].InnerHtml="<p align=center><font color='#003366' class='fon'>"+childdir.Length+"文件夹</font></p>";
string[] childfile=Directory.GetFiles(dir[i].FullName);
this.Table1.Rows[rowcount].Cells[3].InnerHtml="<p align=center><font color='#003366' class='fon'>"+childfile.Length+"文件</font></p>";
rowcount++;
}
for(int i=0;i<files.Length;i++)
{
this.Table1.Rows[rowcount].Cells[0].InnerHtml="<p align=center><font color='#003366' class='fon'>"+files[i].Name+"</font></p>";
this.Table1.Rows[rowcount].Cells[1].InnerHtml="<p> </p>";
this.Table1.Rows[rowcount].Cells[2].InnerHtml="<p> </p>";
this.Table1.Rows[rowcount].Cells[3].InnerHtml="<p> </p>";
rowcount++;
}
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}