Home All Groups Group Topic Archive Search About

Drive Defined - The Dotnet Way?

Author
16 Feb 2006 1:29 PM
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.
--
Donald G Plugge
System Manager
NIH/NCI/EIB Flow Cytometry
(301)435-6429

Author
16 Feb 2006 2:25 PM
Larry Lard
Donald wrote:
> 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?

--
Larry Lard
Replies to group please
Author
16 Feb 2006 3:34 PM
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

Show quote
"Larry Lard" <larryl***@hotmail.com> wrote in message
news:1140099928.516147.193990@g44g2000cwa.googlegroups.com...
>
> Donald wrote:
>> 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?
>
> --
> Larry Lard
> Replies to group please
>
Author
27 Feb 2006 9:41 PM
Richard Grimes [MVP]
Donald wrote:
> 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

AddThis Social Bookmark Button