|
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:
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 Id 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> = <not defined><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 applications /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:
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 Format).
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 Format).
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 Format).
Download
Trial Version (10.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 (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation). |
| 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 ASPSpellCheck Review.
View ASPSpellCheck Examples.
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
|