@@ -54,7 +54,7 @@ public class WindowsPhysicalDiskInfo
5454 /// the given pathname. For example, whether the drive is an SSD or HDD.
5555 /// </summary>
5656 /// <returns>A dictionary of platform-specific keywords and values.</returns>
57- public static Dictionary < string , string > GetPhysicalDiskInfo ( string path )
57+ public static Dictionary < string , string > GetPhysicalDiskInfo ( string path , bool sizeStatsOnly )
5858 {
5959 // Use the WMI APIs to get details about the physical disk associated with the given path.
6060 // Some of these fields are avilable using normal classes, such as System.IO.DriveInfo:
@@ -96,46 +96,14 @@ public static Dictionary<string, string> GetPhysicalDiskInfo(string path)
9696 ManagementScope scope = new ManagementScope ( @"\\.\root\microsoft\windows\storage" ) ;
9797 scope . Connect ( ) ;
9898
99- string queryVolumeString = $ "SELECT DriveType,FileSystem,FileSystemLabel,Size,SizeRemaining FROM MSFT_Volume WHERE DriveLetter=\" { driveLetter } \" ";
100- ManagementBaseObject mbo = GetFirstRecord ( scope , queryVolumeString ) ;
101- if ( mbo != null )
102- {
103- result . Add ( "VolumeDriveType" , GetMapValue ( MapDriveType , FetchValue ( mbo , "DriveType" ) ) ) ;
104- result . Add ( "VolumeFileSystem" , FetchValue ( mbo , "FileSystem" ) ) ;
105- result . Add ( "VolumeFileSystemLabel" , FetchValue ( mbo , "FileSystemLabel" ) ) ;
106- result . Add ( "VolumeSize" , FetchValue ( mbo , "Size" ) ) ;
107- result . Add ( "VolumeSizeRemaining" , FetchValue ( mbo , "SizeRemaining" ) ) ;
108- }
99+ DiskSizeStatistics ( scope , driveLetter , ref result ) ;
109100
110- string queryPartitionString = $ "SELECT DiskNumber FROM MSFT_Partition WHERE DriveLetter=\" { driveLetter } \" ";
111- mbo = GetFirstRecord ( scope , queryPartitionString ) ;
112- if ( mbo != null )
101+ if ( sizeStatsOnly )
113102 {
114- string diskNumber = FetchValue ( mbo , "DiskNumber" ) ;
115- result . Add ( "DiskNumber" , diskNumber ) ;
116-
117- if ( diskNumber . Length > 0 )
118- {
119- string queryDiskString = $ "SELECT Model,IsBoot,IsSystem,SerialNumber FROM MSFT_Disk WHERE Number=\" { diskNumber } \" ";
120- mbo = GetFirstRecord ( scope , queryDiskString ) ;
121- if ( mbo != null )
122- {
123- result . Add ( "DiskModel" , FetchValue ( mbo , "Model" ) ) ;
124- result . Add ( "DiskIsSystem" , FetchValue ( mbo , "IsSystem" ) ) ;
125- result . Add ( "DiskIsBoot" , FetchValue ( mbo , "IsBoot" ) ) ;
126- result . Add ( "DiskSerialNumber" , FetchValue ( mbo , "SerialNumber" ) ) ;
127- }
128-
129- string queryPhysicalDiskString = $ "SELECT MediaType,BusType,SpindleSpeed FROM MSFT_PhysicalDisk WHERE DeviceId=\" { diskNumber } \" ";
130- mbo = GetFirstRecord ( scope , queryPhysicalDiskString ) ;
131- if ( mbo != null )
132- {
133- result . Add ( "PhysicalMediaType" , GetMapValue ( MapMediaType , FetchValue ( mbo , "MediaType" ) ) ) ;
134- result . Add ( "PhysicalBusType" , GetMapValue ( MapBusType , FetchValue ( mbo , "BusType" ) ) ) ;
135- result . Add ( "PhysicalSpindleSpeed" , FetchValue ( mbo , "SpindleSpeed" ) ) ;
136- }
137- }
103+ return result ;
138104 }
105+
106+ DiskTypeInfo ( scope , driveLetter , ref result ) ;
139107 }
140108 catch ( Exception e )
141109 {
@@ -145,6 +113,53 @@ public static Dictionary<string, string> GetPhysicalDiskInfo(string path)
145113 return result ;
146114 }
147115
116+ private static void DiskSizeStatistics ( ManagementScope scope , char driveLetter , ref Dictionary < string , string > result )
117+ {
118+ string queryVolumeString = $ "SELECT DriveType,FileSystem,FileSystemLabel,Size,SizeRemaining FROM MSFT_Volume WHERE DriveLetter=\" { driveLetter } \" ";
119+ ManagementBaseObject mbo = GetFirstRecord ( scope , queryVolumeString ) ;
120+ if ( mbo != null )
121+ {
122+ result . Add ( "VolumeDriveType" , GetMapValue ( MapDriveType , FetchValue ( mbo , "DriveType" ) ) ) ;
123+ result . Add ( "VolumeFileSystem" , FetchValue ( mbo , "FileSystem" ) ) ;
124+ result . Add ( "VolumeFileSystemLabel" , FetchValue ( mbo , "FileSystemLabel" ) ) ;
125+ result . Add ( "VolumeSize" , FetchValue ( mbo , "Size" ) ) ;
126+ result . Add ( "VolumeSizeRemaining" , FetchValue ( mbo , "SizeRemaining" ) ) ;
127+ }
128+ }
129+
130+ private static void DiskTypeInfo ( ManagementScope scope , char driveLetter , ref Dictionary < string , string > result )
131+ {
132+ string queryPartitionString = $ "SELECT DiskNumber FROM MSFT_Partition WHERE DriveLetter=\" { driveLetter } \" ";
133+ ManagementBaseObject mbo = GetFirstRecord ( scope , queryPartitionString ) ;
134+ if ( mbo != null )
135+ {
136+ string diskNumber = FetchValue ( mbo , "DiskNumber" ) ;
137+ result . Add ( "DiskNumber" , diskNumber ) ;
138+
139+ if ( diskNumber . Length > 0 )
140+ {
141+ string queryDiskString = $ "SELECT Model,IsBoot,IsSystem,SerialNumber FROM MSFT_Disk WHERE Number=\" { diskNumber } \" ";
142+ mbo = GetFirstRecord ( scope , queryDiskString ) ;
143+ if ( mbo != null )
144+ {
145+ result . Add ( "DiskModel" , FetchValue ( mbo , "Model" ) ) ;
146+ result . Add ( "DiskIsSystem" , FetchValue ( mbo , "IsSystem" ) ) ;
147+ result . Add ( "DiskIsBoot" , FetchValue ( mbo , "IsBoot" ) ) ;
148+ result . Add ( "DiskSerialNumber" , FetchValue ( mbo , "SerialNumber" ) ) ;
149+ }
150+
151+ string queryPhysicalDiskString = $ "SELECT MediaType,BusType,SpindleSpeed FROM MSFT_PhysicalDisk WHERE DeviceId=\" { diskNumber } \" ";
152+ mbo = GetFirstRecord ( scope , queryPhysicalDiskString ) ;
153+ if ( mbo != null )
154+ {
155+ result . Add ( "PhysicalMediaType" , GetMapValue ( MapMediaType , FetchValue ( mbo , "MediaType" ) ) ) ;
156+ result . Add ( "PhysicalBusType" , GetMapValue ( MapBusType , FetchValue ( mbo , "BusType" ) ) ) ;
157+ result . Add ( "PhysicalSpindleSpeed" , FetchValue ( mbo , "SpindleSpeed" ) ) ;
158+ }
159+ }
160+ }
161+ }
162+
148163 private static string FetchValue ( ManagementBaseObject mbo , string key )
149164 {
150165 return ( mbo [ key ] != null ) ? mbo [ key ] . ToString ( ) . Trim ( ) : string . Empty ;
0 commit comments