ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Visual Basic .NET Unleashed
Visual Basic .NET Unleashed

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

ASP Kitchen: ASP.NET Articles: Accessing Drive Information Using ASP.NET

Accessing Drive Information Using ASP.NET

A Classic ASP article previously demonstrated how to use the Scripting.FileSystemObject object to access information on the drives attached to a web server. The code displays a list of drives attached to the machine, the drive type and whether the drive is available for use. Sample output from the script is shown below:

Sample output from the VBScript Drives Collection Classic ASP script 

Following a great deal of experimentation, I have now converted the code presented in this article to be compatible with ASP.NET. Two different methods are described: using the Scripting.FileSystemObject object, and using Windows Management Instrumentation (WMI).

Displaying Drive Information - Using Scripting.FileSystemObject

The first attempt I made at converting the script to ASP.NET was to simply run through the Classic ASP VBScript version of the script line by line and simply fix any of the compilation errors I was sure I’d encounter due to differences between VBScript and VB.NET. This worked well, with the main issues I encountered being:

  • I had to instantiate all of the variables using the Dim keyword.
  • The Set keyword is no longer supported, so Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject") had to become FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")
  • Select Case statements seem to require a newline character between each Case statement.
  • Response.Write statements now require the string to be enclosed between parentheses.

The ASP.NET version of the script is shown below:

<% 'Script to display a list of drives attached to this machine and also to check if a disk drive is ready Dim FileSystemObject Dim Drives Dim DiskDrive Dim DriveLetter Dim DriveType FileSystemObject = Server.CreateObject("Scripting.FileSystemObject") Drives = FileSystemObject.Drives For Each DiskDrive in Drives DriveLetter = DiskDrive.DriveLetter DriveType = DiskDrive.DriveType Select Case DriveType Case "0" DriveType = "Unknown type of drive" Case "1" DriveType = "Removable drive" Case "2" DriveType = "Fixed drive" Case "3" DriveType = "Network drive" Case "4" DriveType = "CD-ROM drive" Case "5" DriveType = "RAM Disk" End Select Response.Write("Drive " & DriveLetter & " is a " & DriveType & " ") 'If the drive is ready, display a blue piece of text. 'If the drive is not ready, display a red piece of text. If DiskDrive.IsReady then Response.Write("<FONT COLOR=#0000FF>This drive is ready for use</FONT>") Else Response.Write("<FONT COLOR=#FF0000>This drive is not ready for use</FONT>") End If Response.Write("<BR>") Next Drives = nothing FileSystemObject = nothing %>

Although this script works, it relies on using the legacy Scripting.FileSystemObject object. I therefore decided to see if there was an alternative to the Scripting.FileSystemObject object within the .NET Framework.

Displaying Drive Information - Using Windows Management Instrumentation

Browsing through the .NET Framework SDK it became apparent that the most appropriate alternative to using the Scripting.FileSystemObject object was to use Windows Management Instrumentation (WMI). WMI contains extensive facilities for managing Windows and the applications running on Windows machines. Amongst these are facilities for finding out about drives attached to the machine. The Directory.GetLogicalDrives method returns an array containing a list of the drive letters of drives attached to the machine. Once the drives have been determined, it is then possible to determine the drive type by retrieving the value of the DriveType property of the Win32_LogicalDisk WMI class.

The WMI version of the script is shown below. The entire script should be saved as an .aspx page, although you could of course turn it into a control:

<% @Page Language="VB" Debug="true"%> <% @Import Namespace="System"%> <% @Import Namespace="System.IO"%> <% @Import Namespace="System.Management"%> <script language="VB" runat="server"> Sub Page_Load(obj as object, e as eventargs)     'Initialise variables     Dim sSystemDrives as String()     Dim intNumberOfDrives as Integer     Dim sDrive     'Retrieve a list of drives attached to the system     sSystemDrives = Directory.GetLogicalDrives()     'Iterate through the list of drives     For Each sDrive In sSystemDrives         'Drive names are in the format such as A:\, so remove the         'backslash from the drive name         sDrive = Replace(sDrive, "\" ,"")         'Display the drive type (drive type is returned by the         'GetDriveType function         Response.Write("Drive " & sDrive & " is a " & _            GetDriveType(sDrive) & "<br>")     Next     End Sub 'This function uses Windows Management Instrumentation (WMI) 'to return the type of the specified drive 'DriveLetter = Disk drive letter, in a format such as A: 'Returns a string containing the type of drive Function GetDriveType(DriveLetter As String) As String     Dim sDriveType As String     Dim sDriveDescription as String     On Error Resume Next     Dim disk As New ManagementObject("win32_logicaldisk.deviceid=""" & _        DriveLetter & """")     disk.Get()     sDriveType = disk("DriveType").ToString()         'Determine the drive type     Select Case sDriveType         Case "0"             sDriveDescription = "Unknown"         Case "1"             sDriveDescription = "No Root Directory"         Case "2"             sDriveDescription = "Removable Disk"         Case "3"             sDriveDescription = "Local Disk"         Case "4"             sDriveDescription = "Network Drive"         Case "5"             sDriveDescription = "Compact Disc"         Case "6"             sDriveDescription = "RAM Disk"         Case Else             sDriveDescription = "Unknown"     End Select         disk = nothing         GetDriveType = sDriveDescription     End Function </script>

A list of drive type constants is available from the Microsoft website.

The final part of the classic ASP script determined whether or not the drive was ready for use. Using WMI, this could potentially be determined by looking at the Availability, Status or StatusInfo properties. Unfortunately, all three of these properties are not defined in my development environment, so I have been unable to add this functionality to the script (a list of disk properties accessible to WMI can be displayed using this script in this section).

Security Settings

Depending on the security settings on your server, you may not be able to access information using the System.Management namespace. To change this, you may need to add the following to the system.web part of the web.config file:

<authentication mode="Windows"></authentication>
<identity impersonate="true"></identity>

Displaying a Complete Listing of Drive Properties

DriveType is only one of a number of properties that can be returned for a drive. A complete list of properties is to be found on the Microsoft website.

Alternatively, the following script will display a list of properties for a given drive:

<% @Page Language="VB" Debug="true"%> <% @Import Namespace="System"%> <% @Import Namespace="System.IO"%> <% @Import Namespace="System.Management"%> <script language="VB" runat="server"> Sub Page_Load(obj as object, e as eventargs)     On Error Resume Next     'This script uses Windows Management Instrumentation (WMI)     'to return a list of properties for the a specified drive     'attached to the server.     Dim sDriveLetter As String     Dim DiskProperties As PropertyDataCollection     Dim DiskProperty     'Drive letter for which to show drive properties     sDriveLetter = "C:"     Dim disk As New ManagementObject("win32_logicaldisk.deviceid=""" & _       sDriveLetter & """")     disk.Get()         Response.write("<H2>Properties of Drive " & sDriveLetter & "</H2>")     'Retrieve the disk's properties     DiskProperties = disk.Properties     'Iterate through the disk's properties     For Each DiskProperty In DiskProperties                 'Check whether the particular property is defined for this drive         If IsDBNull(DiskProperty.Value.ToString()) Then            Response.Write("<FONT COLOR=""blue"">" & DiskProperty.Name & _                "</FONT> = &lt;not defined&gt;<BR>")         Else                Response.Write("<FONT COLOR=""blue"">" & DiskProperty.Name & _                "</FONT> = <FONT COLOR=""red"">" & _                DiskProperty.Value.ToString() & "</FONT><BR>")         End If                 Next         disk = nothing End Sub </script>

Note that to get this script working, you may need to copy System.Management.dll to your web application’s /bin folder. You might also need to modify permissions on the web files using the code, as well as altering security in IIS. There could be security implications in doing this, so do so at your own risk.

Further reading

  • An ASP disk space monitor. This script uses classic ASP to display disk usage. An ASP.NET version of the script is under development.
  • Help improve your knowledge of ASP.NET (and support this website) by purchasing these books from Amazon.com:

Sams Teach Yourself ASP.NET in 24 Hours ASP.NET Unleashed

Author details

Brett Burridge spent two years working in the University of Essex Computing Service, before moving to The Internet Applications Group in the Autumn of 1999, where he developed e-Business applications for a range of corporate clients and dot-com start ups.

Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited. The company produces a number of innovative products, including the popular ASP Documentation Tool, Index Server Companion, .NET Documentation Tool and SQL Documentation Tool.

As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk and ASPToday.

Contact Brett by emailing

Index Server Companion - allows Index Server to index content from remote websites and ODBC databases!!!

Article history

"Accessing Drive Information Using ASP.NET" published on ASPAlliance.com on 20 March 2002.

ASP Kitchen: ASP.NET Articles: Accessing Drive Information Using ASP.NET

 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 5/17/2008 7:34:09 AM