Web Robot View of http://authors.aspalliance.com/brettb/ASP.NET.DrivesCollection.aspx

Page Item Value
Title ASPAlliance.com : The #1 ASP.NET Developer Community : Accessing Drive Information Using ASP.NET
Description ASP Alliance is the #1 ASP and ASP.NET Community website. The ASP Alliance web site is a free resource for Internet developers, featuring samples, tutorials, and lessons from a variety of industry authors and columnists.
Keywords asp alliance, web, programming, development, magazine, publication, microsoft, visual c, developers, journal, faq, iis, asp, active server pages, asp.net community, internet information server, isapi, ado, odbc, atl, activex, com, sql server, visual basic, vb, cvbscript java, j, jscript, component object model, dcom, scripting, perl, internet database connector, idc, .idc, .asp, channels, .cdf, .cdx, .aspx, asp, aspplus, c#
Robots Meta Tag  
Page Content   The #1 ASP.NET Community All (1065)
Advice (335)
ASP.NET (596)
C# (68)
Classic ASP (249)
Components (80)
Data Access (224)
FAQ (74)
JScript (36)
Sample Code (530)
Tools (177)
Tutorials (438)
VB.NET (67)
Web Services (20)
XML (31)
Mailing Lists
Events Calendar
Community Store
Logo Items Store
.NET User Groups
Technical Forums
Submit Article
Apply For Column
Regular Expressions
Find a Host
.NET Training
MSDN Communities
Advanced Search
Buy/Sell Code
ASP.NET QuickStart
Sample Chapters
Author Interviews
ASP Links
Newsletter Archive
Request an Article
About the Alliance
Advertise
Author List
Contact / Feedback
Link To Us
Privacy
Nancy Abbott
Salman Ahmed
James Avery
Denis Bauer
Brian Bilbro
Robert Boedigheimer
Michael Brinkley
Peter Brunone
Brett Burridge
Alex Campbell
Rob Caron
Rob Chartier
Vladimir Chernetsky
Andy Cheung
Jesudas Chinnathampi (Das)
Jonathan Cogley
Other Contributors
Glenn Cook
Michael Corning
Ken Cox
Brian Desmond
G. Andrew Duthie
Brandon Ellis
Kirk Evans
Chris Garrett
Jason Gaylord
Paul Glavich
David Gottlieb
Nakul Goyal
Ed Habal
Ralf Hansen
Csaba Hatvany
Tim Heuer
Darius Hurdle
Anjum Iqbal
Thomas Johansen
Peter Johnson
Scott Kallmeyer
Teemu Keiski
Lin Ken
Kumar Gaurav Khanna
Aaron King
Prasad Kunisetty
Andreas Kviby
Colt Kwong
Tin Lam
Eric Landes
J. Ambrose Little
Alex Lowe
Bjørn Lyngwa
Eric Madariaga
Don Makoviney
Haroon Malik
Damian Manifold
Andrew Mooney
Tim Morford
Tim Musschoot
Anand Narayanaswamy
Darren Neimke
Christian Nordbakk
Jeff Nuckolls
Rey Nuñez
Daniel Olson
Steve Presley
Philip Quinn
Rajiv R
Christopher Rickard
Lance Robinson
Kyle Roche
Jim Ross
Vegas Sairaj
Jason Salas
Nathan Schmoll
Bharat Sharma
Steve Sharrock
Sumeet Singh
Andy Smith
Steven Smith
Gregg Stark
Dr. Christopher Sully
Steven Swafford
Justin Thomas
Ryan Trudelle-Schwarz
Michiel van Otegem
Terry Voss
Tom Warren
Gordon Weis
Keith and King Wells
Dennis West
Jeff Widmer
Paul Wilson
Yusuf Wiryonoputro
Remas Wojciechowski
David Yack
Greg Zinger
Jeremy Zongker
A1VBCode
Amundsen.com
angryCoder.com
ASP Alliance
ASP Authors
ASP.NET FAQ
ASPDLL
AspItalia.com
ASPNL
ASPNL (Dutch)
ASPSmith
ASPSpider
aspZone.com
BinaryIntellect
BipinJoshi
Code101.com
ConnectionStrings.com
CoverYourASP
CSharp-Station.com
DataWebControls.com
DevBuilder
DeveloperFusion
DotNetBips
DotNetGerman
DotNetJohn
DotNetSlackers
DotNetSpider
DotNetToolbox
GotCodeSnippets
IISDEV
INETA
Kapoor Solutions
kbAlertz.com
Minwar.com
MSWebDev
NGallery.org
Project Distributor
RegExLib
SampleCodePool
ScottOnWriting
StarDeveloper
StartVBDotNet
SteveOrr
TopXML
W3Coder
W3Schools - ASP/ASP.NET
WWWCoder.com
XMLforASP.NET
ASPAlliance.com Site Current Users: Pages Last Week: Sessions Last Week: Pages Last Month: Sessions Last Month: --> ASPAlliance Ad Network Ads Last 30 Days: 69881002 [More Stats] Stats Source: --> (Advertise Here) --> featured columnist --> Peter Brunone Just click here. Pure C# Find Prices Sample Chapter 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 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:

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 and the recently released Index Server Companion and ASP.NET Documentation Tool . The company is also available for web application development at reasonable rates, primarily using Microsoft technologies (ASP, Visual Basic, SQL Server) but also using open source technologies such as PHP, MySQL and Perl. Specialist services include development of search solutions using Microsoft's Index Server and Site Server 3.0 Search. As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk and ASPToday .

Join IT Banner Exchange.com

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 4/28/2006 12:06:24 PM
Image Alt Tags ASPAlliance.com : The #1 Active Server Pages .NET Community
Search
Search
Subscribe
Subscribe
ArticleTopics
Powered by ORCSWeb Hosting
Community
Resources
Other Info
Columnists
Partner Sites
Site Stats
Powered By ASP.NET
Featured Sponsor
Featured Columnist
Peter Brunone
Featured Book
Pure C#
New! asp.netPRO
We publish our articles in the standard RSS format.
Powerful .NET Email Component
Code Sharing Software
Sample output from the VBScript Drives Collection Classic ASP script
Sams Teach Yourself ASP.NET in 24 Hours
ASP.NET Unleashed
Index Server Companion - allows Index Server to index content from remote websites and ODBC databases!!!
Internal Links http://authors.aspalliance.com/brettb/ASP.NET.DrivesCollection.aspx (3 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/ASP.NetArticles.aspx (2 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/Default.asp (2 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/VBScriptDrivesCollection.asp [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/ASPDiskMonitoringScript.asp [ Robot View of this URL ]
http://authors.aspalliance.com/libraryaspa/logocounter.asp [ Robot View of this URL ]
http://authors.aspalliance.com/chapters/0672322668/0672322668_ch03.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/BookPrice.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/Columnist.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/DisplayColumnistPhoto.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/ [ Robot View of this URL ]

Reporting Main Page

Report generated by The Website Utility 2.8