There are advanced classes in .NET for IO processing. Even though we have some classes that allow us to carry out low level IO operations, we can usually do our work with several static methods. Now let's see how a directory and a file is created, deleted and how directory hierarchy in a disk volume or in a directory can be displayed in tree view. Although IO operations are same in all .NET projects, to make easier our article I use ASP.NET. Hence you can easily copy and paste code below in, for example, WPF. Classes, we will use, are:
· DriveInfo
· DirectoryInfo
· FileInfo
· Directory
· File
To create a directory or file
Directory and File classes in System.IO namespace allow us to create and delete directories or files:
Directory.Create(@"C:\exampleDirectory");
Directory.Delete(@"C:\exampleDirectory");
If we want to work on files and directories in current project folder,
Directory.Create(@"Server.MapPath("exampleDirectory"));
Directory.Delete(@"Server.MapPath("exampleDirectory"));
we can use. When operating on files, use File class:
File.Create(@"C:\exampleFile.doc")
File.Delete(@"C:\exampleFile.doc")
or
File.Create(@"Server.MapPath("exampleFile"));
File.Delete(@"Server.MapPath("exampleFile"));
Now let's work on displaying directory and file hierarchy in treeView like "windows explorer" style. We will use these classes:
· System.IO.DriveInfo //allows us to access the name, available space, total space etc. of a disk volume
· System.IO.DirectoryInfo //allows us to access the name, available space, total space etc. of directory
Create a new site, add TreeView and wrote below code in Page_Load event:
rotected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.IO.DriveInfo drive = new System.IO.DriveInfo(@"D:\");
//work on local disk D
TreeNode node = new TreeNode();
//we create TreeNode's dinamically
node.Value = drive.Name;
TreeView1.Nodes.Add(node);
//add local disk D to main node
loadDirectories (node, drive.Name);//in this recursive method,
// we pass name and full address of the local disk
// as parameters
}
}
private void loadDirectories(TreeNode parent, string path)
{
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(path);
//now we don't work on drives but directories
try
{
//with GetDirectories(), we get all sub directories
foreach (System.IO.DirectoryInfo d in directory.GetDirectories())
{
TreeNode node = new TreeNode(d.Name, d.FullName);
parent.ChildNodes.Add(node);
//call the same method (call "loadDirectories()" recursively )
loadDirectories(node, d.FullName);
}
}
catch (System.UnauthorizedAccessException e)
{
parent.Text += "(acces forbiddeni)";
}
catch (System.IO.IOException e)
{
parent.Text += "(unknown error hata: " + e.Message + ")";
}
}
We displayed just content of volume D by writing ”System.IO.DriveInfo drive = new System.IO.DriveInfo(@"D:\");". If we wanted to display all files and directories in computer hard disk, we would write:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
//to work on all volumes
{
TreeNode node = new TreeNode();
node.Value = drive.Name;
if (drive.IsReady)
{
node.Text = drive.Name;
loadDirectories(node, drive.Name);
}
TreeView1.Nodes.Add(node);
}
}
}
The code below takes several minutes. As you can estimate, our recursive method "loadDirectories()" needs many IO operations.
Now add a GridView to the project. We want to select a file from the TreeView we just created, and see alll subfiles and subdirectories with their name, space, last access time etc. Select SelectedNodeChanged event in "Properties Window" after clicking on TreeView and write the code below.
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(TreeView1.SelectedNode.Value);
GridView1.DataSource = directory.GetFiles();
GridView1.DataBind();
}
We learned how to create and delete files and directories and displaying all subdirectories and files in a disk, hierarchically in a TreeView.