ASP.NET 例程完全代码版(2)——DirectoryInfo类

似乎能在.NET Framework 1.1下跑的程序,到了.NET 2.0出了问题,在baidu搜了搜,也有人问同样的问题——Directory是静态类,当然不能实例化,可是资料上有些程序却是这么写的Directory dir = new Directory("strDir");如此在VS 2005中编译是通不过的!有些书籍确是垃圾,作者只是为了赚钱而写书,对技术一点不付责任哦,书上代码都跑不过,更不用说是附带的光盘里的了。
     下面这个例子实现了“我的电脑”的功能,呵呵,其实,就是由驱动器到目录到文件的查看和文件预览,代码不是很规范,不过可以对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>");
    }
第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();
        } 
    }
第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]">
<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为启动页,运行即可!


本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6968,如需转载请自行联系原作者
上一篇:《MVP时间》之物联网产品未来的发展方向


下一篇:Python 技术篇-socket套接字实现两个窗口间消息传递实例演示,TCP实现