Drive Defined - The Dotnet Way?

D

Donald

In both VB6 and VB.NET I can search through the available drives and look
for a specific drive type (ie fixed, CD, remote). This is done with the
File System Object and other scripting objects along with a for loop:

Dim fso As New Scripting.FileSystemObject

Dim Drives As Scripting.Drives = fso.Drives

Dim Drive As Scripting.Drive

For Each Drive In Drives

If (Drive.DriveType = Scripting.DriveTypeConst.Fixed) Then

*** do something with local fixed drive only **

End If

Next

However, what if I know the drive I want to check. How do I assign the
proper letter to a single drive instance? It appears that the methods for
the drive class are read only and somehow assigned values internally during
the for loop assignment. I can't create a drive object for a given drive
letter and then access the methods like "drivetypecont" on that single
drive. What is the dotnet solution to this issue? Not just a work around.
I have a work around already.
 
L

Larry Lard

Donald said:
However, what if I know the drive I want to check. How do I assign the
proper letter to a single drive instance?

I found this sample on MSDN, hopefully it will help you look in the
right places:This example finds the amount of free disk space on the C: drive of the
local computer.

Example
Public Function GetDiskSpace() As System.UInt64
Dim diskClass As _
New System.Management.ManagementClass("Win32_LogicalDisk")
Dim disks As System.Management.ManagementObjectCollection = _
diskClass.GetInstances()
Dim disk As System.Management.ManagementObject
Dim space As System.UInt64
For Each disk In disks
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
Next disk
Return space
End Function
Messy, huh?
 
D

Donald

Larry,

Thanks for the example. Yes, I did look at and use the Management Class,
previously. However, as you say, "Messy". I can't imagine that this is the
"dotnet way". Maybe version 2.0 will have an answer.

Don
 
R

Richard Grimes [MVP]

Donald said:
Larry,

Thanks for the example. Yes, I did look at and use the Management
Class, previously. However, as you say, "Messy". I can't imagine
that this is the "dotnet way". Maybe version 2.0 will have an answer.

It is worse than messy. I was absolutely aghast when I first saw that
Microsoft recommended WMI to access what a drive type is when a p/invoke
call is simple. WMI is fine when you have *no other option*. The problem
with WMI is that you have to make a COM interop call to a local COM
server. That is, you have to ask another process. If you use pinvoke to
the Win32 functions you make a call to a DLL that is already loaded in
your process. Luckily .NET 2.0 has a new class called DriveInfo that
gives you access to drives.

I rant a bit about this issue in my security workshop:

http://www.grimes.demon.co.uk/workshops/secWSSeven.htm#custom_cas_permission

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top