asp.net - How to get file names in a directory

Get names of files in a specified directory
.Net framework Directory.GetFiles() method allow us to return the names of files in the specified directory. files name included their path also. Directory class GetFiles(String) method exists in System.IO namespace.

GetFiles() method require to pass a parameter named 'path'. the 'path' parameter value data type is System.String which represents the relative or absolute path to the directory to search. this string is not case-sensitive.

GetFiles() method return value type is System.String[] which represents an array of the files full name in the specified directory or an empty array if no files are found in the specified directory. each file name included its path also.

GetFiles() method throw IOException exception, if 'path' is a file name or a network error has occurred. method throw UnauthorizedAccessException, if the caller does not have the required permission. it throw ArgumentNullException, if 'path' is null.

method throw DirectoryNotFoundException, if the specified path is not found or is invalid. this method also throw ArgumentException and PathTooLongException.

the following asp.net c# example code demonstrate us how can we get all files name in a specified directory programmatically at run time in an asp.net application.
DirectoryGetFiles.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        string path = Request.PhysicalApplicationPath;
        TextBox1.Text = path;
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string myDirectory = TextBox1.Text.ToString();
        string[] directories = Directory.GetFiles(myDirectory);
        ListBox1.DataSource = directories;
        ListBox1.DataBind();
        ListBox1.Height = 250;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get file list under a directory in asp.net programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net example: directory file list</h2>
        <asp:ListBox 
             ID="ListBox1" 
             runat="server" 
             BackColor="Teal" 
             ForeColor="Snow"
             >
        </asp:ListBox>
        <br /><br />
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Text="Directory"
            ForeColor="DarkGreen"
            Font-Bold="true"
            >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server" 
             ReadOnly="true"
             BackColor="DarkGreen"
             ForeColor="Ivory"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="DarkBlue"
            Text="Show Directory File List"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>


More asp.net examples