下面这个例子实现了“我的电脑”的功能,呵呵,其实,就是由驱动器到目录到文件的查看和文件预览,代码不是很规范,不过可以对Directory & DirectoryInfo 有个大概的理解。
工程建立,在VS 2005中建立web site,共三个页面ListDrives.aspx,ListDir.aspx,ShowFile.aspx。下面是三个页面的代码:
第一个ListDrives.aspx:
只写了个 Page_Load 事件,页面加载的时候得到所有驱动器的列表。
protected void Page_Load(object sender, EventArgs e)
{
string[] achDrives = Directory.GetLogicalDrives();
Response.Write("<ul>");
for (int i = 0; i < achDrives.Length; i++)
{
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(achDrives));
Response.Write("\">" + achDrives);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
{
string[] achDrives = Directory.GetLogicalDrives();
Response.Write("<ul>");
for (int i = 0; i < achDrives.Length; i++)
{
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(achDrives));
Response.Write("\">" + achDrives);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
第2个页面,同样也是写了一个Page_Load( 事件,通过得到传递的驱动器名称得到相应的目录文件
protected void Page_Load(object sender, EventArgs e)
{
string dir = Request.QueryString.Get("dir");
try
{
DirectoryInfo directory = new DirectoryInfo(dir);
Response.Write("<p>Creation: " + directory.CreationTime.ToString() + "</p>");
Response.Write("<ul>");
DirectoryInfo[] subDirectory = directory.GetDirectories(); //所有子目录
for (int i = 0; i < subDirectory.Length; i++)
{
Response.Write("<li><a href = \"ListDir.aspx?dir="); //继续进入子目录……
Response.Write(Server.UrlEncode(subDirectory.FullName));
Response.Write("\">" + subDirectory.Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
FileInfo[] theFiles = directory.GetFiles(); //所有非目录文件
for (int i = 0; i < theFiles.Length; i++)
{
Response.Write("<li><a href = \"ShowFile.aspx?file=");
Response.Write(Server.UrlEncode(theFiles.FullName));
Response.Write("\">" + theFiles.Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
catch (Exception ex)
{
Response.Write("Access not possible, error: <i>");
Response.Write(ex.ToString() + "</i>");
Response.End();
{
string dir = Request.QueryString.Get("dir");
try
{
DirectoryInfo directory = new DirectoryInfo(dir);
Response.Write("<p>Creation: " + directory.CreationTime.ToString() + "</p>");
Response.Write("<ul>");
DirectoryInfo[] subDirectory = directory.GetDirectories(); //所有子目录
for (int i = 0; i < subDirectory.Length; i++)
{
Response.Write("<li><a href = \"ListDir.aspx?dir="); //继续进入子目录……
Response.Write(Server.UrlEncode(subDirectory.FullName));
Response.Write("\">" + subDirectory.Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
FileInfo[] theFiles = directory.GetFiles(); //所有非目录文件
for (int i = 0; i < theFiles.Length; i++)
{
Response.Write("<li><a href = \"ShowFile.aspx?file=");
Response.Write(Server.UrlEncode(theFiles.FullName));
Response.Write("\">" + theFiles.Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
catch (Exception ex)
{
Response.Write("Access not possible, error: <i>");
Response.Write(ex.ToString() + "</i>");
Response.End();
}
}
}
第3个文件ShowFile.aspx,这个页面中布局一个HtmlTable,用来显示相应的文件信息。下面是在它的HTML代码,读者可以通过它看到Table的结构,非常简单。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowFile.aspx.cs" Inherits="ShowFile" %>
<%@Import Namespace= "System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<%@Import Namespace= "System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="[url]http://www.w3.org/1999/xhtml[/url]" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<%
string file2Show = Request.QueryString.Get("file");
FileInfo thisOne = new FileInfo(file2Show);
%>
<div>
<table style="width: 731px; height: 293px">
<tr>
<td style="width: 191px">
name
</td>
<td><%= thisOne.Name%>
</td>
<td style="width: 237px">
create date</td>
<td>
</td>
</tr>
<tr>
<td style="width: 191px">
path</td>
<td><%= thisOne.FullName%>
</td>
<td style="width: 237px">
size</td>
<td><%=thisOne.Length.ToString()%>
</td>
</tr>
<tr>
<td style="width: 191px">
directory</td>
<td><%= thisOne.DirectoryName%>
</td>
<td style="width: 237px">
last access</td>
<td><%= thisOne.LastAccessTime.ToString()%>
</td>
</tr>
<tr>
<td style="width: 191px; height: 71px;">
last modified</td>
<td style="height: 71px"><%= thisOne.LastWriteTime.ToString()%>
</td>
<td style="width: 237px; height: 71px;">
</td>
<td style="height: 71px">
</td>
</tr>
</table>
</div>
<%
StreamReader reader = thisOne.OpenText();
char[] buffer = new char[1000];
int nRead = reader.ReadBlock(buffer,0,255);
Response.Write("<pre>");
Response.Write(new string(buffer,0,nRead));
Response.Write("</pre>");
%>
</body>
</html>
OK,到此设置ListDrives.aspx为启动页,运行即可!
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<%
string file2Show = Request.QueryString.Get("file");
FileInfo thisOne = new FileInfo(file2Show);
%>
<div>
<table style="width: 731px; height: 293px">
<tr>
<td style="width: 191px">
name
</td>
<td><%= thisOne.Name%>
</td>
<td style="width: 237px">
create date</td>
<td>
</td>
</tr>
<tr>
<td style="width: 191px">
path</td>
<td><%= thisOne.FullName%>
</td>
<td style="width: 237px">
size</td>
<td><%=thisOne.Length.ToString()%>
</td>
</tr>
<tr>
<td style="width: 191px">
directory</td>
<td><%= thisOne.DirectoryName%>
</td>
<td style="width: 237px">
last access</td>
<td><%= thisOne.LastAccessTime.ToString()%>
</td>
</tr>
<tr>
<td style="width: 191px; height: 71px;">
last modified</td>
<td style="height: 71px"><%= thisOne.LastWriteTime.ToString()%>
</td>
<td style="width: 237px; height: 71px;">
</td>
<td style="height: 71px">
</td>
</tr>
</table>
</div>
<%
StreamReader reader = thisOne.OpenText();
char[] buffer = new char[1000];
int nRead = reader.ReadBlock(buffer,0,255);
Response.Write("<pre>");
Response.Write(new string(buffer,0,nRead));
Response.Write("</pre>");
%>
</body>
</html>
OK,到此设置ListDrives.aspx为启动页,运行即可!
本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6968,如需转载请自行联系原作者