ASPAlliance ASP Kitchen  
Search: Go  

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

Useful Development Tools

ASP Documentation Tool™
Automatically creates technical documentation for ASP 2.0 and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (5.2Mb ZIP file).

.NET Documentation Tool
Automatically creates technical documentation for .NET Framework applications written in C# or VB.NET (including ASP.NET). Documentation for SQL Server 7/2000/2005 databases and C#/VB.NET components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. Additional support for ASP.NET web applications. A useful alternative to the popular NDoc code documentor.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (3Mb ZIP file).

SQL Documentation Tool
The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0, 2000 and 2005 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers, Table Relationships, Jobs and DTS Packages.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (10.3Mb ZIP file).

VB Documentation Tool
The VB Documentation Tool creates technical documentation for Microsoft Visual Basic 6.0 projects. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced.
   View Sample Output (HTML Help format) View Sample Output (HTML Help format).
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (1Mb ZIP file).

The Website Utility
The Website Utility examines websites for errors and areas that need to be optimised for search engines by using a built in web crawling engine. Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags. It also generates HTML for use in creating website site maps (table of contents pages - like this one), and is able to create both client-side JavaScript search engines and server-side ASP search engines and ASP.NET search engines for a website.
   View Sample Output (HTML Format) View Sample Output (HTML Format).
   Download Trial Version Download Trial Version (3Mb ZIP file).

Text Workbench
Text Workbench is a file search and replacement utility for text files and Microsoft Office documents. Make rapid file replacements on multiple files and folders full of files. Advanced replacement options include regular expressions support. It even works on remote file systems via FTP. A Regular Expression Laboratory allows advanced pattern matching and replacement expressions to be built and tested. This great utility will make your everyday development tasks much easier!
   Download Trial Version of Text Workbench Download Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).

Indexing Service Companion
The Indexing Service Companion is a utility that extends the functionality of the Microsoft Windows Indexing Service so that it is able to index content from any remote website and also from ODBC compliant databases. As such it can be used as a low cost alternative to Sharepoint's Search Services.
   View Product Documentation View Product Documentation (119K ZIP file).
   Try Sample Search Facility Try Sample Search Facility.
   Download Trial Version Download Trial Version (1.7Mb ZIP file).

ASP Spell Check
ASPSpellCheck is the easy way to add spell checking capabilities to your ASP or ASP.NET websites, Intranets and web applications. The utility allows you to add spell checking capabilities to any HTML text field or rich content editing text box. It works with all common web browsers, and there are no components or databases to install on the server.
   Read a review of the ASP Spell Check server component Read ASPSpellCheck Review.
   View Examples of the ASPSpellCheck component for adding spell checking capabilities to ASP web applications View ASPSpellCheck Examples.
   Download Trial Version of ASPSpellCheck Download Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).

Author details

Brett Burridge has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments.

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 a range of software documentation tools, which include the ASP Documentation Tool™, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application.

As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk, ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook.    links

Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde.

Contact Brett by emailing

Documentation tools to automate the documentation of SQL Server databases and ASP, C#, VB.NET and VB 6.0 application source code

Download a Free ASP Documentation Tool Now!

Google

Search Engine Builder - Build a search engine for your website!

© page content copyright Brett Burridge 1998 - 2008.