@@ -20,49 +20,40 @@ internal class UsbFactory
2020 /// </summary>
2121 /// <returns>A List of UsbDisk instances.</returns>
2222
23- public List < UsbDisk > GetAvailableDisks ( )
23+ public IEnumerable < UsbDisk > GetAvailableDisks ( )
2424 {
25- var disks = new List < UsbDisk > ( ) ;
25+ using var searcher = new ManagementObjectSearcher (
26+ "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'" ) . Get ( ) ;
2627
27- using ( var searcher = new ManagementObjectSearcher (
28- "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'" ) . Get ( ) )
28+ // browse all USB WMI physical disks
29+ foreach ( var o in searcher )
2930 {
30- // browse all USB WMI physical disks
31- foreach ( var o in searcher )
32- {
33- var drive = ( ManagementObject ) o ;
34- // associate physical disks with partitions
35- using ( var partition = new ManagementObjectSearcher (
36- $ "associators of {{Win32_DiskDrive.DeviceID='{ drive [ "DeviceID" ] } '}} where AssocClass = Win32_DiskDriveToDiskPartition") . First ( ) )
37- {
38- if ( partition == null ) continue ;
31+ var drive = ( ManagementObject ) o ;
3932
40- // associate partitions with logical disks (drive letter volumes)
41- using ( var logical = new ManagementObjectSearcher (
42- $ "associators of {{Win32_DiskPartition.DeviceID='{ partition [ "DeviceID" ] } '}} where AssocClass = Win32_LogicalDiskToPartition") . First ( ) )
43- {
44- if ( logical == null ) continue ;
33+ // associate physical disks with partitions
34+ using var partition = new ManagementObjectSearcher (
35+ $ "associators of {{Win32_DiskDrive.DeviceID='{ drive [ "DeviceID" ] } '}} where AssocClass = Win32_DiskDriveToDiskPartition") . First ( ) ;
4536
46- // finally find the logical disk entry to determine the volume name
47- using ( var volume = new ManagementObjectSearcher (
48- $ "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{ logical [ "Name" ] } '") . First ( ) )
49- {
50- var disk = new UsbDisk ( logical [ "Name" ] . ToString ( ) )
51- {
52- Model = drive [ "Model" ] . ToString ( ) ,
53- Volume = volume [ "VolumeName" ] . ToString ( ) ,
54- FreeSpace = ( ulong ) volume [ "FreeSpace" ] ,
55- Size = ( ulong ) volume [ "Size" ]
56- } ;
37+ if ( partition == null ) continue ;
5738
58- disks . Add ( disk ) ;
59- }
60- }
61- }
62- }
63- }
39+ // associate partitions with logical disks (drive letter volumes)
40+ using var logical = new ManagementObjectSearcher (
41+ $ "associators of {{Win32_DiskPartition.DeviceID='{ partition [ "DeviceID" ] } '}} where AssocClass = Win32_LogicalDiskToPartition") . First ( ) ;
42+
43+ if ( logical == null ) continue ;
6444
65- return disks ;
45+ // finally find the logical disk entry to determine the volume name
46+ using var volume = new ManagementObjectSearcher (
47+ $ "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{ logical [ "Name" ] } '") . First ( ) ;
48+
49+ yield return new UsbDisk ( logical [ "Name" ] . ToString ( ) )
50+ {
51+ Model = drive [ "Model" ] . ToString ( ) ,
52+ Volume = volume [ "VolumeName" ] . ToString ( ) ,
53+ FreeSpace = ( ulong ) volume [ "FreeSpace" ] ,
54+ Size = ( ulong ) volume [ "Size" ]
55+ } ;
56+ }
6657 }
6758 }
6859}
0 commit comments