Working With Drives and Directories in ASP.NET

In this article we will discuss how to interact with the File System, Drives and Directories and how we get the path details. The System.IO namespace is used to work with files and directories of ASP.Net. Here we use the DriveInfo class of the System.IO namespace. It provides extended information of any drive.

First we discuss Drive and Directories. For this follow these steps:

1. First, we use some Labels to show the information:

    Drive Name:
        <asp:Label ID="lblDriveName" runat="server" Text="Label"></asp:Label>

        <br />
    Avaliable free Space   
    <asp:Label ID="lblAvaliableFreeSpace" runat="server" Text="Label"></asp:Label>

    <br />
    Drive Type:
        <asp:Label ID="lblDriveType" runat="server" Text="Label"></asp:Label>

        <br />
    Drive Format:
        <asp:Label ID="lblDriveFormat" runat="server" Text="Label"></asp:Label>

        <br />
        Total Free Space Avaliable in the Drive:
        <asp:Label ID="lblTotalFreeSpace" runat="server" Text="Label"></asp:Label>

        <br />
        Total Size:
        <asp:Label ID="lblTotalSize" runat="server" Text="Label"></asp:Label>

        <br />

2. After that, we write the code on the Page_Load() event in the .cs page:

protected void Page_Load(object sender, EventArgs e)

    System.IO.DriveInfo MyDrive = new System.IO.DriveInfo(@"D:\");

    lblDriveName.Text = MyDrive.Name;
    lblDriveFormat.Text = MyDrive.DriveFormat;
    lblAvaliableFreeSpace.Text = MyDrive.AvailableFreeSpace.ToString();
    lblDriveType.Text = MyDrive.DriveType.ToString();
    lblTotalFreeSpace.Text = MyDrive.TotalFreeSpace.ToString();

Here we get the Information of the (D:\) drive. We can change the drive in:

System.IO.DriveInfo MyDrive = new System.IO.DriveInfo(@"D:\");

We can change it like this:

System.IO.DriveInfo MyDrive = new System.IO.DriveInfo(@"C:\");

In this example we get the Drive name with the Name property. Like this we can get more information such as the format of the drive, available free size etc.

The Output will Be:


Now we discuss how to get the path details of the particular path. For this use these steps:

Step 1: First we use a TextBox, where we specify the PathName:

<asp:TextBox ID="txtpathname" runat="server"></asp:TextBox>

Step 2: After that, we take some Labels and a Button to show the information regarding the path:


Step 3: Now we write the code on the OnClick Event of the Button Control in the .cs page:

protected void btnGetDetails_Click(object sender, EventArgs e)
    lblRootPath.Text = System.IO.Path.GetFullPath(txtpathname.Text);
    lblFileName.Text = System.IO.Path.GetFileName(txtpathname.Text);
    lblFileDirectory.Text = System.IO.Path.GetDirectoryName(txtpathname.Text);

lblExtension.Text = System.IO.Path.GetExtension(txtpathname.Text);
lblFileNameWithoutExtension.Text = System.IO.Path.GetFileNameWithoutExtension(txtpathname.Text);


Recommended Ebook

ASP.NET MVC 5: A Beginner’s Guide

Download Now!
Similar Articles