Viewing SubDirectories of a Directory
In this article I'm trying to explain how to view sub directories based on root directory. In my previous articles I'm trying to explain how to view the files in a directory and how to copy files from one directory to another etc.. but now I'm trying to explain how to view sub directories available under root directory. This article will help you especially for fresher's and those who are new to this concept.
Viewing SubDirectories of a Directory:
In this article I'm trying to explain how to view sub directories based on root directory. In my previous articles I'm trying to explain how to view the files in a directory and how to copy files from one directory to another etc.. but now I'm trying to explain how to view sub directories available under root directory. This article will help you especially for fresher's and those who are new to this concept.
Follow below steps to achieve your goal.Step-1:
Create a project and then right click on solution explorer then choose Add-> New Item then choose Webforms and give a name for that as "ViewSubDir.aspx" and then click on Ok button. Step-2:
Once page created successfully then design the page based on our requirement. What we want exactly, we need to display all the subdirectories entered by user. For entering root directory purpose we simply drag and drop 1 textbox control after that we need to perform one action for that simply drag and drop one button control and we want to show all the subdirectories into one control like (ListBox, Dropdown, DataControls …). In my application I just consider one ListBox control.Step-3:
After design the page design view looks becomes like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewSubDir.aspx.cs" Inherits="ASPnet_ViewSubDir" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblSubDir" runat="server"
Text="Enter Path and Name of Sub Directory : "></asp:Label>
<asp:TextBox ID="txtSubDir" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSubDir" runat="server" onclick="btnSubDir_Click"
Text="Show SubDirectories" />
<br />
<br />
<br />
<asp:ListBox ID="lstSubDir" runat="server"></asp:ListBox>
<br />
<br />
</div>
</form>
</body>
</html>Step-4:
Onclick event of button wrote below lines of code.
protected void btnSubDir_Click(object sender, EventArgs e)
{
//fetch root directory entered by user
string Path = txtSubDir.Text;
System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(Path);
//check that root directory exists or not
if (rootDir.Exists)
{
//check available sub directories under that root directory
System.IO.DirectoryInfo[] subDir = rootDir.GetDirectories();
//add all the subdirectories into listbox control
for (int i = 0; i < subDir.Length; i++)
{
lstSubDir.Items.Add(subDir[i].ToString());
}
}
else
{
lstSubDir.Items.Add("Root Diectory doesn't exists..!!!");
}
}Step-5:
Now, everything is ready to perform the action. Once just click on F5 on keyboard then see the result set.
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ASPnet_ViewSubDir : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubDir_Click(object sender, EventArgs e)
{
string Path = txtSubDir.Text;
System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(Path);
if (rootDir.Exists)
{
System.IO.DirectoryInfo[] subDir = rootDir.GetDirectories();
for (int i = 0; i < subDir.Length; i++)
{
lstSubDir.Items.Add(subDir[i].ToString());
}
}
else
{
lstSubDir.Items.Add("Root Diectory doesn't exists..!!!");
}
}
}Conclusion:
This article will help you for fetching sub directories in our system directories and display that into our controls. Hope this will help you for freshers and those who are beginners to use this.