使用 asp.net 和 C# 列出目录中的文件夹

发布于 2024-11-08 02:09:15 字数 1208 浏览 1 评论 0原文

.aspx 文件:

<%@ Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Explorer</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>

.CS 文件:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.IO;

public partial class view2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string path = "~/";
    GetFilesFromDirectory(path);
}

private static void GetFilesFromDirectory(string DirPath)
{
         try
         {
             DirectoryInfo Dir = new DirectoryInfo(DirPath);
             FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
             foreach (FileInfo FI in FileList)
             {
                 Console.WriteLine(FI.FullName);
             }
         }
         catch (Exception ex)
         {
                Console.WriteLine(ex.Message);
         }
}

我想列出特定目录中的文件夹,但它不断显示空白页。任何人都可以告诉代码中的问题是什么。

.aspx file:

<%@ Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Explorer</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>

.CS file:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.IO;

public partial class view2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string path = "~/";
    GetFilesFromDirectory(path);
}

private static void GetFilesFromDirectory(string DirPath)
{
         try
         {
             DirectoryInfo Dir = new DirectoryInfo(DirPath);
             FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
             foreach (FileInfo FI in FileList)
             {
                 Console.WriteLine(FI.FullName);
             }
         }
         catch (Exception ex)
         {
                Console.WriteLine(ex.Message);
         }
}

I want to list the folders in a particular directory but it continuously showing blank page.Can anybody tell what's the problem in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白衬杉格子梦 2024-11-15 02:09:15

在空白页上显示目录和文件

// YourPage.aspx
<%@ Import Namespace="System.IO" %>
<html>
<body>
    <% foreach (var dir in new DirectoryInfo("E:\\TEMP").GetDirectories()) { %>
        Directory: <%= dir.Name %><br />

        <% foreach (var file in dir.GetFiles()) { %>
            <%= file.Name %><br />
        <% } %>
        <br />
    <% } %>
</body>
</html>

Display directories and files on a blank page

// YourPage.aspx
<%@ Import Namespace="System.IO" %>
<html>
<body>
    <% foreach (var dir in new DirectoryInfo("E:\\TEMP").GetDirectories()) { %>
        Directory: <%= dir.Name %><br />

        <% foreach (var file in dir.GetFiles()) { %>
            <%= file.Name %><br />
        <% } %>
        <br />
    <% } %>
</body>
</html>
铃予 2024-11-15 02:09:15

不要使用 Console.WriteLine(),而使用 Response.Write()。您正在尝试写入 Web 应用程序中的控制台。

Don't use Console.WriteLine() use Response.Write(). You're trying to write to the console in a web application.

草莓酥 2024-11-15 02:09:15

Console.WriteLine 将写入控制台,而不是您返回的网页内容。您需要将一个容器元素添加到您的 ASPX 页面,可能是一个网格视图或转发器,然后从代码隐藏文件中添加分配文件列表(对于您添加的 HTML 元素,使用 runat='server' 标记并为其分配一个ID,然后在代码中通过ID名称引用它)。

Console.WriteLine will write to the console, not the web page contents you are returning. You need to add a container element to your ASPX page, probably a grid view or repeater, then add assign the file list from the code behind file (to the HTML element you added, use the runat='server' tag and assign it an ID, then reference it by ID name in the code).

倒数 2024-11-15 02:09:15

Response.Write 在静态代码隐藏方法中:脏!另外你也没有控制你写的位置。这个比较干净一点...

// YourPage.aspx
<%@ Import Namespace="System.IO" %>
<html>
<body>
    <ul>
        <% foreach(var file in Directory.GetFiles("C:\\Temp", "*.*", SearchOption.AllDirectories)) { %>
        <li><%= file %></li>       
        <% } %>     
    </ul>
</body>
</html>

Response.Write in a static codebehind method: DIRTY! In addition you did't control the position where you write. This a little bit cleaner...

// YourPage.aspx
<%@ Import Namespace="System.IO" %>
<html>
<body>
    <ul>
        <% foreach(var file in Directory.GetFiles("C:\\Temp", "*.*", SearchOption.AllDirectories)) { %>
        <li><%= file %></li>       
        <% } %>     
    </ul>
</body>
</html>
抱猫软卧 2024-11-15 02:09:15

您可以使用 Directory 类

  • 第一个参数是路径,可以是相对路径或绝对路径
  • 第二个参数用于匹配路径中子目录的名称。此参数可以包含有效文字和通配符的组合,但它不支持正则表达式。

/

//using System.IO; 
private void GetDirectories()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("direction",typeof(string));
    try
    {
        string[] dirs = Directory.GetDirectories(@"yourpath", "*", SearchOption.AllDirectories);
        foreach (string dir in dirs)
        {
            dt.Rows.Add(dir);
        }
        if (dirs.Length <= 0)
        {
             lbl.text="your message"

        }

       rpt.DataSource = dt; //your repeater 
       rpt.DataBind(); //your repeater 
    }
    catch (Exception e)
    {
       lbl.text="your message"//print message assign it to label
    }
}

在aspx页面中

   <asp:Label runat="server" ID="lbl"></asp:Label>
    <asp:Repeater ID="rpt" runat="server" ClientIDMode="AutoID">
        <ItemTemplate>
            <tr>
                <td><%#Eval("direction")%></td>

            </tr>
        </ItemTemplate>
    </asp:Repeater>

You can use Directory class

  • The first parameter is the path it can be relative or absolute
  • The second parameter for matching against the names of subdirectories in path. This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.

/

//using System.IO; 
private void GetDirectories()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("direction",typeof(string));
    try
    {
        string[] dirs = Directory.GetDirectories(@"yourpath", "*", SearchOption.AllDirectories);
        foreach (string dir in dirs)
        {
            dt.Rows.Add(dir);
        }
        if (dirs.Length <= 0)
        {
             lbl.text="your message"

        }

       rpt.DataSource = dt; //your repeater 
       rpt.DataBind(); //your repeater 
    }
    catch (Exception e)
    {
       lbl.text="your message"//print message assign it to label
    }
}

In the aspx page

   <asp:Label runat="server" ID="lbl"></asp:Label>
    <asp:Repeater ID="rpt" runat="server" ClientIDMode="AutoID">
        <ItemTemplate>
            <tr>
                <td><%#Eval("direction")%></td>

            </tr>
        </ItemTemplate>
    </asp:Repeater>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文