Working with files & folders in ASP.Net applications - Iterate through local file system drives
This is a second post in a series regarding ASP.Net and manipulation of files & folders. You can find the other one here .
In this post we will try to iterate through all local file system drives.
I will use Visual Studio 2010 Ultimate edition and VB.Net as the development language.
1) Create an ASP.Net site and name it as you like
2) Add a new page in your web site. Name it IterateDriverInfo.aspx.Add a Treeview control on the page.
<table>
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
</td>
</tr>
</table>
3) In the Page_Load event handling routine type
If (Not Page.IsPostBack) Then
For Each drive As DriveInfo In DriveInfo.GetDrives()
Dim node As TreeNode = New TreeNode()
node.Value = drive.Name
If (drive.IsReady) Then
node.Text = drive.Name & _
" - (The free space available is: " & drive.AvailableFreeSpace & ")"
Else
node.Text = drive.Name & " - (The drive is not ready)"
End If
Me.TreeView1.Nodes.Add(node)
Next
End If
4) In the above snippet we do use the GetDrives() method of the DriveInfo class.Something important to note is that we should check the drive object's IsReady property to see
whether the drive is accessible.Besides the drive name we also get the free available bytes per drive.
5) Run your application and see what the information looks like in the browser
Hope it helps!!!