|
-
Apr 18th, 2003, 11:40 PM
#1
Thread Starter
Member
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
-
Apr 18th, 2003, 11:47 PM
#2
Fanatic Member
The FileSystem object *is* the .NET way to do it. What exactly is the problem with FSO?
-
Apr 18th, 2003, 11:57 PM
#3
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.
-
Apr 19th, 2003, 12:51 AM
#4
Thread Starter
Member
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.
-
Apr 19th, 2003, 01:27 AM
#5
Fanatic Member
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.
-
Apr 19th, 2003, 01:52 AM
#6
-
Apr 19th, 2003, 02:30 AM
#7
Here is a trimmed up example:
VB Code:
Imports System.Management
Public Function VolumeName(ByVal driveltr As String) As String
'trim off ending \ if its there
If driveltr.EndsWith("\") Then driveltr = driveltr.Substring(0, driveltr.Length - 1)
'driveltr should be c: or d:
Dim disk As New ManagementObject("Win32_Logicaldisk='" & driveltr & "'")
Return disk.Properties("VolumeName").Value.ToString()
End Function
Requires a reference to System.Management also.
-
Apr 19th, 2003, 03:09 AM
#8
Fanatic Member
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.
-
Apr 19th, 2003, 06:21 AM
#9
Thread Starter
Member
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.
-
Apr 19th, 2003, 12:09 PM
#10
Fanatic Member
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.
-
Apr 19th, 2003, 04:05 PM
#11
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.
-
Apr 19th, 2003, 04:55 PM
#12
Run this on a form with a listbox to see all the available properties of the ManagementObject, one of which is 'FileSystem':
VB Code:
Me.Cursor = Cursors.WaitCursor
Dim path As String = "Win32_Logicaldisk='C:'"
Dim mo As New ManagementObject(path)
Dim al As New ArrayList()
Dim pd As PropertyData
For Each pd In mo.Properties
al.Add(pd.Name.ToString)
Next
ListBox1.DataSource = al
Me.Cursor = Cursors.Default
-
Apr 19th, 2003, 05:07 PM
#13
Wow I learned quite a bit through this. Here is a short helper class for you:
VB Code:
Public Class DiskInfo
Public Enum DiskProperties
Access
Availability
BlockSize
Caption
Compressed
ConfigManagerErrorCode
ConfigManagerUserConfig
CreationClassName
Description
DeviceID
DriveType
ErrorCleared
ErrorDescription
ErrorMethodology
FileSystem
FreeSpace
InstallDate
LastErrorCode
MaximumComponentLength
MediaType
Name
NumberOfBlocks
PNPDeviceID
PowerManagementCapabilities
PowerManagementSupported
ProviderName
Purpose
QuotasDisabled
QuotasIncomplete
QuotasRebuilding
Size
Status
StatusInfo
SupportsDiskQuotas
SupportsFileBasedCompression
SystemCreationClassName
SystemName
VolumeDirty
VolumeName
VolumeSerialNumber
End Enum
Public Shared Function GetProperty(ByVal driveltr As String, ByVal propertyname As DiskProperties) As Object
'trim off ending \ if its there
If driveltr.EndsWith("\") Then driveltr = driveltr.Substring(0, driveltr.Length - 1)
'driveltr should be c: or d:
Dim disk As New ManagementObject("Win32_Logicaldisk='" & driveltr & "'")
Return disk.Properties(propertyname.ToString).Value
End Function
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:
MsgBox(DiskInfo.GetProperty("C:", DiskInfo.DiskProperties.FileSystem), , DiskInfo.DiskProperties.FileSystem.ToString)
-
Apr 19th, 2003, 05:42 PM
#14
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|