Results 1 to 14 of 14

Thread: Volume Name & File System

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53

    Volume Name & File System

    I'm currently using the FSO (FileSystem Object) to get a drives volume name and/or the file system type (FAT32/NTFS).

    Anyone know a '.NET' way to do this?

    I'd like to be able to achieve the above WITHOUT using FSO.

    Thanks

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    The FileSystem object *is* the .NET way to do it. What exactly is the problem with FSO?

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually the FileSystemObject is left over from VB6, although you can still use it. If you want the .NET way then check out the IO namespace.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53
    It most certainly is NOT the .NET way, i've used FSO for quite a while when back on VB6. I never did like it.

    I would only use FSO if I had too, but I can't believe there's no option in .NET to get the file system or volume name, I've looked, just can't find it yet.

    Concerning FSO, here's Microsofts stance on it in an optimization article of theirs:

    The traditional runtime file functions are provided for compatibility with earlier version of Vidual Basic. FSO is provided for compatibility with scripting languages, and for applications that require it's functionality. Each of these models is implemented as a set of wrapper objects that call members of classes in the system.IO namespace. Therefore, they are not as efficient as using system.IO directly. In addition, FSO requires your application to implement COM interop, which incurs marshalling overhead.

    Anyway, using FSO drops a DLL with my exe file, hate that too, lol.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Woops, I was confusing FileSystemInfo with FileSystemObject.
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemiofilesysteminfoclasstopic.htm
    ...which does not appear to offer help with obtaining volume name or filesystem type, and neither does anything else in the framework docs appear to. Sorry for the confusion.

    I guess one way (not a good way) would be to shell a command line utility and examine the output, or you might use the GetVolumeInformation API call (faster but more painful). Cut and pasted from API-Guide, in VB6:

    Code:
    Private Declare Function GetVolumeInformation Lib "Kernel32" _
    Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
    ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As _
    Long, lpVolumeSerialNumber As Long, _
    lpMaximumComponentLength As Long, lpFileSystemFlags As _
    Long, ByVal lpFileSystemNameBuffer As String, ByVal _
    nFileSystemNameSize As Long) As Long
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim Serial As Long, VName As String, FSName As String
        'Create buffers
        VName = String$(255, Chr$(0))
        FSName = String$(255, Chr$(0))
        'Get the volume information
        GetVolumeInformation "C:\", VName, 255, Serial, 0, 0, FSName, 255
        'Strip the extra chr$(0)'s
        VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
        FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
        MsgBox "The Volume name of C:\ is '" + VName + _
        "', the File system name of C:\ is '" + FSName + _
        "' and the serial number of C:\ is '" + Trim(Str$(Serial)) + _
        "'", vbInformation + vbOKOnly, App.Title
    End Sub

    Code:
    'Example by Alexey ([email protected])
    Private Declare Function GetVolumeInformation Lib "Kernel32" _
    Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
    ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As _
    Long, lpVolumeSerialNumber As Long, _
    lpMaximumComponentLength As Long, lpFileSystemFlags As _
    Long, ByVal lpFileSystemNameBuffer As String, ByVal _
    nFileSystemNameSize As Long) As Long
    Private Const MAX_FILENAME_LEN = 256
    Public Function DriveSerial(ByVal sDrv As String) As Long
        Dim RetVal As Long
        Dim str As String * MAX_FILENAME_LEN
        Dim str2 As String * MAX_FILENAME_LEN
        Dim a As Long
        Dim b As Long
        Call GetVolumeInformation(sDrv & ":\", str, MAX_FILENAME_LEN, RetVal, a, b, str2, MAX_FILENAME_LEN)
        DriveSerial = RetVal
    End Function
    Private Sub Form_Load()
        MsgBox "Serial of drive C is " & DriveSerial("C")
    End Sub
    From the docs for the function:
    · lpRootPathName
    Points to a string that contains the root directory of the volume to be described. If this parameter is NULL, the root of the current directory is used. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\.

    · lpVolumeNameBuffer
    Points to a buffer that receives the name of the specified volume.


    · nVolumeNameSize
    Specifies the length, in characters, of the volume name buffer. This parameter is ignored if the volume name buffer is not supplied.

    · lpVolumeSerialNumber
    Points to a variable that receives the volume serial number. This parameter can be NULL if the serial number is not required.

    · lpMaximumComponentLength
    Points to a doubleword value that receives the maximum length, in characters, of a filename component supported by the specified file system. A filename component is that portion of a filename between backslashes.
    The value stored in variable pointed to by *lpMaximumComponentLength is used to indicate that long names are supported by the specified file system. For example, for a FAT file system supporting long names, the function stores the value 255, rather than the previous 8.3 indicator. Long names can also be supported on systems that use the New Technology file system.

    · lpFileSystemFlags
    Points to a doubleword that receives flags associated with the specified file system. This parameter can be any combination of the following flags, with one exception: FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually exclusive.
    FS_CASE_IS_PRESERVED
    If this flag is set, the file system preserves the case of filenames when it places a name on disk.
    FS_CASE_SENSITIVE
    If this flag is set, the file system supports case-sensitive filenames.
    FS_UNICODE_STORED_ON_DISK
    If this flag is set, the file system supports Unicode in filenames as they appear on disk.
    FS_PERSISTENT_ACLS
    If this flag is set, the file system preserves and enforces ACLs. For example, NTFS preserves and enforces ACLs, and FAT does not.
    FS_FILE_COMPRESSION
    The file system supports file-based compression.
    FS_VOL_IS_COMPRESSED
    The specified volume is a compressed volume; for example, a DoubleSpace volume.

    · lpFileSystemNameBuffer
    Points to a buffer that receives the name of the file system (such as FAT or NTFS).


    · nFileSystemNameSize
    Specifies the length, in characters, of the file system name buffer. This parameter is ignored if the file system name buffer is not supplied.
    Last edited by Slow_Learner; Apr 19th, 2003 at 01:37 AM.

  6. #6

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is a trimmed up example:
    VB Code:
    1. Imports System.Management
    2.  
    3.     Public Function VolumeName(ByVal driveltr As String) As String
    4.         'trim off ending \ if its there
    5.         If driveltr.EndsWith("\") Then driveltr = driveltr.Substring(0, driveltr.Length - 1)
    6.         'driveltr should be c: or d:
    7.         Dim disk As New ManagementObject("Win32_Logicaldisk='" & driveltr & "'")
    8.         Return disk.Properties("VolumeName").Value.ToString()
    9.     End Function

    Requires a reference to System.Management also.

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Beats the carp out of using the API to do it, although you would have to be careful that the target machine has WMI installed.

  9. #9

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53
    The API version I can't seem to convert to .NET, the WMI version looks nice, but does that allow you to find 'FileSystem' too?
    Last edited by JustAProg; Apr 21st, 2003 at 09:14 AM.

  10. #10
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I would be surprised if there isn't an equivalent, although of course edneeis would know for sure. I'm looking for a list of WMI exposed functions too, looks like the API way is the old-n-crummy.

    edit: gwaaa, I have the WMI SDK docs installed on my machine already! where the hell did that come from! *slugs coffee* Aha part of the platform SDK!

    edit2: aha here's sample code for a WMI schema browser:
    ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconbrowsingthewmischema.htm
    guess I need to shell out some money on a WMI book
    Last edited by Slow_Learner; Apr 19th, 2003 at 12:28 PM.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I believe the WMI code is basically a wrapper for the API. The properties it exposes can be found in some API structures. I'll try to find out for ya.

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Run this on a form with a listbox to see all the available properties of the ManagementObject, one of which is 'FileSystem':

    VB Code:
    1. Me.Cursor = Cursors.WaitCursor
    2.         Dim path As String = "Win32_Logicaldisk='C:'"
    3.         Dim mo As New ManagementObject(path)
    4.         Dim al As New ArrayList()
    5.         Dim pd As PropertyData
    6.         For Each pd In mo.Properties
    7.             al.Add(pd.Name.ToString)
    8.         Next
    9.         ListBox1.DataSource = al
    10.         Me.Cursor = Cursors.Default

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Wow I learned quite a bit through this. Here is a short helper class for you:

    VB Code:
    1. Public Class DiskInfo
    2.  
    3.     Public Enum DiskProperties
    4.         Access
    5.         Availability
    6.         BlockSize
    7.         Caption
    8.         Compressed
    9.         ConfigManagerErrorCode
    10.         ConfigManagerUserConfig
    11.         CreationClassName
    12.         Description
    13.         DeviceID
    14.         DriveType
    15.         ErrorCleared
    16.         ErrorDescription
    17.         ErrorMethodology
    18.         FileSystem
    19.         FreeSpace
    20.         InstallDate
    21.         LastErrorCode
    22.         MaximumComponentLength
    23.         MediaType
    24.         Name
    25.         NumberOfBlocks
    26.         PNPDeviceID
    27.         PowerManagementCapabilities
    28.         PowerManagementSupported
    29.         ProviderName
    30.         Purpose
    31.         QuotasDisabled
    32.         QuotasIncomplete
    33.         QuotasRebuilding
    34.         Size
    35.         Status
    36.         StatusInfo
    37.         SupportsDiskQuotas
    38.         SupportsFileBasedCompression
    39.         SystemCreationClassName
    40.         SystemName
    41.         VolumeDirty
    42.         VolumeName
    43.         VolumeSerialNumber
    44.     End Enum
    45.  
    46.     Public Shared Function GetProperty(ByVal driveltr As String, ByVal propertyname As DiskProperties) As Object
    47.         'trim off ending \ if its there
    48.         If driveltr.EndsWith("\") Then driveltr = driveltr.Substring(0, driveltr.Length - 1)
    49.         'driveltr should be c: or d:
    50.         Dim disk As New ManagementObject("Win32_Logicaldisk='" & driveltr & "'")
    51.         Return disk.Properties(propertyname.ToString).Value
    52.     End Function
    53.  
    54. End Class

    It could be converted into a more appropriate class where the constructor loads teh management object and fills properties or better yet you could just use the Management Object directly. The enum is all of the available properties. Here is the syntax for using the helper class:

    VB Code:
    1. MsgBox(DiskInfo.GetProperty("C:", DiskInfo.DiskProperties.FileSystem), , DiskInfo.DiskProperties.FileSystem.ToString)

  14. #14
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Aha, THIS is more like what I was looking for:
    ms-help://MS.VSCC/MS.MSDNVS/wmisdk/r_32hard1_3d4j.htm

    A straight up list of what you can retrieve, although I haven't found the other classes besides Win32. Most of the system-management stuff (the things I used to work with back when I made money) are under Operating System Classes:

    ms-help://MS.VSCC/MS.MSDNVS/wmisdk/r_32os1_12yb.htm

    The Platform SDK docs are very nasty compared to the docs included with the IDE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width