5

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

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

4

5 回答 5

14

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

// 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>
于 2011-05-18T16:17:58.757 回答
1

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

于 2011-05-18T15:38:58.133 回答
1

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>
于 2011-05-18T15:46:01.857 回答
1

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

于 2011-05-18T15:36:48.303 回答
0

您可以使用目录类

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

/

//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>
于 2019-10-29T23:27:12.793 回答