Listing Folders in a Directory using asp.net and C#

后端 未结 5 1986
你的背包
你的背包 2020-12-11 07:58

.aspx file:

<%@ Import Namespace=\"System.IO\" %>




        
相关标签:
5条回答
  • 2020-12-11 08:35

    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>
    
    0 讨论(0)
  • 2020-12-11 08:36

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

    0 讨论(0)
  • 2020-12-11 08:46

    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>
    
    0 讨论(0)
  • 2020-12-11 08:55

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

    0 讨论(0)
  • 2020-12-11 08:58

    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>
    
    0 讨论(0)
提交回复
热议问题