日期:2011-10-03 浏览次数:20499 次
Asp.net IO类目录操作实例 完成取得驱动器列表 显示目录下的子目录和文件 读取文件内容 显示文件信息
IODirectory.aspx.cs代码(实现驱动器目录列表)
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string[] drives=Directory.GetLogicalDrives();
int numofdrives=drives.Length;
Response.Write("<ul>");
for(int i=0;i<numofdrives;i++){
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(drives[i]));
Response.Write("\">"+drives[i]);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
listdir.aspx.cs代码(显示目录下的子目录和文件)
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strdirlist=Request.QueryString.Get("dir");
DirectoryInfo directory=null;
try{
directory=new DirectoryInfo(strdirlist);
//读取目录属性
Response.Write("<p>Creation:"+directory.CreationTime.ToString()+"</p>");
DirectoryInfo[] subDirectory=directory.GetDirectories();
Response.Write("<ul>");
for(int i=0;i<subDirectory.Length;i++){
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(subDirectory[i].FullName));
Response.Write("\">"+subDirectory[i].Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
FileInfo[] theFiles=directory.GetFiles();//可以使用GetFiles("*.txt")进行文件搜索
Response.Write("<ul>");
for(int i=0;i<theFiles.Length;i++){
Response.Write("<li><a href=\"showfile.aspx?file=");
Response.Write(Server.UrlEncode(theFiles[i].FullName));
Response.Write("\">"+theFiles[i].Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
catch(Exception ex){
Response.Write("Access not possible. error:<i>");
Response.Write(e.ToString()+"</i>");
Response.End();
}
}
showfile.aspx.cs代码(显示文件内容)
public string strFileshow;
public FileInfo thisOne;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Encoding ed=Encoding.GetEncoding("gb2312");
Response.ContentEncoding=ed;
Request.ContentEncoding=ed;
strFileshow=Request.QueryString.Get("file");
thisOne=new FileInfo(strFileshow);
Response.Write("文件名:"+thisOne.Name+"<br>");
Response.Write("文件路径:"+thisOne.FullName+"<br>");
Response.Write("文件目录:"+thisOne.DirectoryName+"<br>");
Response.Write("文件建立时间:"+thisOne.CreationTime.ToString()+"<br>");
Response.Write("文件大小:"+thisOne.Length+"Bytes<br>");
Response.Write("最后访问时