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

|| .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);
         }
}
我想在特定目录中列出文件夹,但该文件夹连续显示空白页。任何人都可以在代码中说出问题所在。     
已邀请:
在空白页上显示目录和文件
// 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>
    
不要使用
Console.WriteLine()
来使用
Response.Write()
。您正在尝试在Web应用程序中写入控制台。     
Console.WriteLine将写入控制台,而不是您返回的网页内容。您需要将容器元素添加到ASPX页面(可能是网格视图或转发器),然后从文件后面的代码中分配文件列表(对于您添加的HTML元素,使用runat = \'server \'标记并分配它是一个ID,然后在代码中按ID名称引用它)。     
Response。在静态代码隐藏方法中写入: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>
    
您可以使用目录类 第一个参数是相对或绝对路径 第二个参数,用于匹配path中子目录的名称。此参数可以包含有效文字和通配符的组合,但不支持正则表达式。 /
//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>
    

要回复问题请先登录注册