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,如需转载请自行联系原作者
相关文章
|
6月前
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
192 8
|
4月前
|
JSON 安全 API
.net 自定义日志类
在.NET中,创建自定义日志类有助于更好地管理日志信息。示例展示了如何创建、配置和使用日志记录功能,包括写入日志文件、设置日志级别、格式化消息等。注意事项涵盖时间戳、日志级别、JSON序列化、线程安全、日志格式、文件处理及示例使用。请根据需求调整代码。
76 13
|
4月前
|
算法 Java 测试技术
使用 BenchmarkDotNet 对 .NET 代码进行性能基准测试
使用 BenchmarkDotNet 对 .NET 代码进行性能基准测试
110 13
|
4月前
|
JSON 数据格式
.net HTTP请求类封装
`HttpRequestHelper` 是一个用于简化 HTTP 请求的辅助类,支持发送 GET 和 POST 请求。它使用 `HttpClient` 发起请求,并通过 `Newtonsoft.Json` 处理 JSON 数据。示例展示了如何使用该类发送请求并处理响应。注意事项包括:简单的错误处理、需安装 `Newtonsoft.Json` 依赖,以及建议重用 `HttpClient` 实例以优化性能。
118 2
|
4月前
|
开发框架 .NET PHP
ASP.NET Web Pages - 添加 Razor 代码
ASP.NET Web Pages 使用 Razor 标记添加服务器端代码,支持 C# 和 Visual Basic。Razor 语法简洁易学,类似于 ASP 和 PHP。例如,在网页中加入 `@DateTime.Now` 可以实时显示当前时间。
|
5月前
|
敏捷开发 缓存 中间件
.NET技术的高效开发模式,涵盖面向对象编程、良好架构设计及高效代码编写与管理三大关键要素
本文深入探讨了.NET技术的高效开发模式,涵盖面向对象编程、良好架构设计及高效代码编写与管理三大关键要素,并通过企业级应用和Web应用开发的实践案例,展示了如何在实际项目中应用这些模式,旨在为开发者提供有益的参考和指导。
67 3
|
6月前
.NET 4.0下实现.NET4.5的Task类相似功能组件
【10月更文挑战第29天】在.NET 4.0 环境下,可以使用 `BackgroundWorker` 类来实现类似于 .NET 4.5 中 `Task` 类的功能。`BackgroundWorker` 允许在后台执行耗时操作,同时不会阻塞用户界面线程,并支持进度报告和取消操作。尽管它有一些局限性,如复杂的事件处理模型和不灵活的任务管理方式,但在某些情况下仍能有效替代 `Task` 类。
|
6月前
|
前端开发 JavaScript C#
CodeMaid:一款基于.NET开发的Visual Studio代码简化和整理实用插件
CodeMaid:一款基于.NET开发的Visual Studio代码简化和整理实用插件
152 0
|
6月前
|
API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
117 0
|
8月前
|
Kubernetes 监控 Devops
【独家揭秘】.NET项目中的DevOps实践:从代码提交到生产部署,你不知道的那些事!
【8月更文挑战第28天】.NET 项目中的 DevOps 实践贯穿代码提交到生产部署全流程,涵盖健壮的源代码管理、GitFlow 工作流、持续集成与部署、容器化及监控日志记录。通过 Git、CI/CD 工具、Kubernetes 及日志框架的最佳实践应用,显著提升软件开发效率与质量。本文通过具体示例,助力开发者构建高效可靠的 DevOps 流程,确保项目成功交付。
151 0