【问题标题】:Listing Folders in a Directory using asp.net and C#使用 asp.net 和 C# 列出目录中的文件夹
【发布时间】:2011-05-18 15:29:06
【问题描述】:

.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);
         }
}

我想列出特定目录中的文件夹,但它一直显示空白页。谁能告诉代码中的问题。

【问题讨论】:

    标签: c# asp.net list directory getfiles


    【解决方案1】:

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

    // 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>
    

    【讨论】:

    • 我正在尝试做类似的事情,但我不想使用静态路径。有没有办法获取应用程序路径?例如“~/Uploads/”,Uploads 是我的网络项目中的一个文件夹。我不想这样:“C://Visual Studio 2013/projects/my project”等
    • @Ra3IDeN 是的,使用 HttpContext.Current.Server.MapPath(StringPathInSolution) ,但是由于您的项目路径是静态的,因此知道您可以将其从字符串中删除?
    • 在我的情况下它不获取文件
    • 正确!你是专业人士!
    【解决方案2】:

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

    【讨论】:

    • 我收到此错误:非静态字段、方法或属性“System.Web.UI.Page.Response.get”需要对象引用
    • @ILLUMINATI7590:哦,从private static void GetFilesFromDirectory(string DirPath) 中删除static。在这种情况下你不需要它。
    • 谢谢它删除了错误。但我的网页仍然没有显示任何内容。
    • @ILLUMINATI7590:你确定没有抛出错误吗?您是否替换了Console.WriteLine() 的两个实例?
    • @ILLUMINATI7590:如果这对你不起作用,而本杰明做到了,你应该将答案授予他,而不是我。我相信他能够帮助您编写代码。 :)
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      Response.Write 在静态代码隐藏方法中:DIRTY!另外你没有控制你写的位置。这有点清洁...

      // 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>
      

      【讨论】:

      • 你有你的&lt;li&gt;&lt;ul&gt; mixed up. Also, I do agree with you but the user seems to just be doing something basic right now (the page is blank and there's nothing else in the code-behind) so I didn't really see the point of getting into perfecting the display. Also, I didn't even notice that the method was static`,好眼睛。 :)
      • 非常感谢。我是编码新手。
      • 请告诉我这个。我只想显示目录中的文件夹(而不是子文件夹)。在你的代码中,用目录替换文件,给出其中的每个文件夹。
      【解决方案5】:

      你可以使用目录类

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

      /

      //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>
      

      【讨论】:

        猜你喜欢
        • 2013-12-25
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2010-12-22
        • 2011-06-29
        • 2011-01-23
        • 1970-01-01
        • 2016-01-28
        相关资源
        最近更新 更多