Get names of sub directories in a directory
.Net framework Directory.GetDirectories(String) method allow us to get the names of sub directories including
their paths in the specified directory. Directory class GetDirectories() method exists in System.IO namespace.
GetDirectories(String) method has a required 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 path string is not case-sensitive.
GetDirectories(String) method return value type is System.String[] which represents an array of the full names of sub directories in the specified path. method returns an empty array if no directories are found in specified directory.
GetDirectories(String) method throw UnauthorizedAccessException, if the caller does not have the required permission. it throw ArgumentNullException exception, if the 'path' is null. method throw IOException, if the 'path' is a file name.
method throw DirectoryNotFoundException, if the specified path is invalid. method also throw ArgumentException and PathTooLongException.
Directory.GetDirectories(String, String) overloaded method allow us to get the names of sub directories including their paths that match the specified search pattern in the specified directory.
Directory.GetDirectories(String, String, SearchOption) overloaded method allow us to get the names of sub directories including their paths that match the specified search pattern in the specified directory and optionally searches sub directories.
the following asp.net c# example code demonstrate us how can we get names of sub directories in a specified directory programmatically at run time in an asp.net application.
GetDirectories(String) method has a required 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 path string is not case-sensitive.
GetDirectories(String) method return value type is System.String[] which represents an array of the full names of sub directories in the specified path. method returns an empty array if no directories are found in specified directory.
GetDirectories(String) method throw UnauthorizedAccessException, if the caller does not have the required permission. it throw ArgumentNullException exception, if the 'path' is null. method throw IOException, if the 'path' is a file name.
method throw DirectoryNotFoundException, if the specified path is invalid. method also throw ArgumentException and PathTooLongException.
Directory.GetDirectories(String, String) overloaded method allow us to get the names of sub directories including their paths that match the specified search pattern in the specified directory.
Directory.GetDirectories(String, String, SearchOption) overloaded method allow us to get the names of sub directories including their paths that match the specified search pattern in the specified directory and optionally searches sub directories.
the following asp.net c# example code demonstrate us how can we get names of sub directories in a specified directory programmatically at run time in an asp.net application.
DirectoryGetDirectories.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
string path = Request.PhysicalApplicationPath + "Images";
TextBox1.Text = path;
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
string myDirectory = TextBox1.Text.ToString();
string[] directories = Directory.GetDirectories(myDirectory);
ListBox1.DataSource = directories;
ListBox1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to get sub directory 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 sub directories</h2>
<asp:ListBox
ID="ListBox1"
runat="server"
BackColor="OrangeRed"
ForeColor="Snow"
>
</asp:ListBox>
<br /><br />
<asp:Label
ID="Label1"
runat="server"
Text="Directory"
ForeColor="DodgerBlue"
Font-Bold="true"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
ReadOnly="true"
BackColor="DodgerBlue"
ForeColor="AliceBlue"
>
</asp:TextBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="Crimson"
Text="Show Sub Directory List"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>


