@@ -14,79 +14,77 @@ public static class DeviceInfo
1414 public static float ReadIO { get ; private set ; }
1515 public static float WriteIO { get ; private set ; }
1616
17- public static List < Device > LogicalDrives { get ; private set ; }
18- private static Dictionary < string , DeviceIoControl > DeviceIoList ;
19- private static int CountDiscs = 0 ;
20-
21- private static Performance [ ] Perfomances ;
22- private static float [ ] PrevReadValues ;
23- private static float [ ] PrevWriteValues ;
24-
25- private static void SetDiscCollection ( )
17+ public static List < Device > LogicalDrives { get ; }
18+ private static Dictionary < string , DeviceIoControl > _deviceIoList ;
19+
20+ private static Performance [ ] _perfomances ;
21+ private static float [ ] _prevReadValues ;
22+ private static float [ ] _prevWriteValues ;
23+
24+ static DeviceInfo ( )
2625 {
2726 LogicalDrives = new ( ) ;
28- DeviceIoList = new ( ) ;
29-
30- IEnumerable < LogicalDrive > logicalDevicesIO = DeviceIoControl . GetLogicalDrives ( ) ;
31-
32- CountDiscs = logicalDevicesIO . Count ( ) ;
27+ _deviceIoList = new ( ) ;
28+ }
3329
34- byte index = 0 ;
30+ private static void SetDiscCollection ( )
31+ {
32+ LogicalDrives . Clear ( ) ;
33+ _deviceIoList . Clear ( ) ;
3534
36- PrevReadValues = new float [ logicalDevicesIO . Count ( ) ] ;
37- PrevWriteValues = new float [ PrevReadValues . Length ] ;
38- Perfomances = new Performance [ PrevReadValues . Length ] ;
35+ IEnumerable < LogicalDrive > logicalDevicesIo = DeviceIoControl . GetLogicalDrives ( ) ;
3936
40- foreach ( LogicalDrive logical in logicalDevicesIO )
37+ foreach ( LogicalDrive logical in logicalDevicesIo )
4138 {
42- if ( logical . Type != WinApi . DRIVE . FIXED ) continue ;
43-
44- DeviceIoControl deviceIO = new ( logical . Name ) ;
45-
46- // if (!deviceIO.FileSystem.IsVolumeMounted || deviceIO.Storage.MediaTypesEx.DeviceType != StorageApi.STORAGE_DEVICE_NUMBER.FileDevice.DISK)
47- // {
48- // deviceIO.Dispose();
49- // continue;
50- // }
39+ if ( logical . Type != WinApi . DRIVE . FIXED )
40+ continue ;
5141
5242 Device device = new ( ) ;
5343 device . Letter = logical . Name ;
5444
55- var smart = deviceIO . Disc . Smart ;
45+ LogicalDrives . Add ( device ) ;
46+ _deviceIoList . Add ( device . Letter , new DeviceIoControl ( device . Letter ) ) ;
47+ }
5648
57- if ( smart != null ) device . Model = smart . SystemParams . ModelNumber ;
58- else device . Model = new DriveInfo ( logical . Name ) . VolumeLabel ;
49+ byte index = 0 ;
5950
60- if ( device . Model . Contains ( "samsung" , StringComparison . OrdinalIgnoreCase ) ) device . Vendor = VendorEnum . Samsung ;
61- else if ( device . Model . Contains ( "kingston" , StringComparison . OrdinalIgnoreCase ) ) device . Vendor = VendorEnum . Kingston ;
62- else if ( device . Model . Contains ( "wdc" , StringComparison . OrdinalIgnoreCase ) ) device . Vendor = VendorEnum . WDC ;
63- else device . Vendor = VendorEnum . Other ;
51+ _prevReadValues = new float [ LogicalDrives . Count ] ;
52+ _prevWriteValues = new float [ LogicalDrives . Count ] ;
53+ _perfomances = new Performance [ LogicalDrives . Count ] ;
6454
65- LogicalDrives . Add ( device ) ;
55+ foreach ( Device dev in LogicalDrives )
56+ {
57+ var smart = _deviceIoList [ dev . Letter ] . Disc . Smart ;
6658
67- DeviceIoList . Add ( device . Letter , deviceIO ) ;
59+ if ( smart != null ) dev . Model = smart . SystemParams . ModelNumber . Trim ( ' ' ) ;
60+ else dev . Model = new DriveInfo ( dev . Letter ) . VolumeLabel ;
6861
69- Perfomances [ index ] = deviceIO . Disc . GetDiscPerformance ( ) ;
62+ if ( dev . Model . Contains ( "samsung" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . Samsung ;
63+ else if ( dev . Model . Contains ( "kingston" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . Kingston ;
64+ else if ( dev . Model . Contains ( "wdc" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . WDC ;
65+ else dev . Vendor = VendorEnum . Other ;
66+
67+ _perfomances [ index ] = _deviceIoList [ dev . Letter ] . Disc . GetDiscPerformance ( ) ;
7068
71- DiscApi . DISK_PERFORMANCE dISK_PERFORMANCE = Perfomances [ index ] . QueryPerformanceInfo ( ) ;
69+ DiscApi . DISK_PERFORMANCE diskPerformance = _perfomances [ index ] . QueryPerformanceInfo ( ) ;
7270
73- PrevReadValues [ index ] = dISK_PERFORMANCE . BytesRead ;
74- PrevWriteValues [ index ] = dISK_PERFORMANCE . BytesWritten ;
71+ _prevReadValues [ index ] = diskPerformance . BytesRead ;
72+ _prevWriteValues [ index ] = diskPerformance . BytesWritten ;
7573
7674 index ++ ;
7775 }
7876 }
7977
8078 public static bool IsUpdateDiscs ( )
8179 {
82- if ( CountDiscs != DeviceIoControl . GetLogicalDrives ( ) . Count ( ) )
80+ if ( LogicalDrives . Count != DeviceIoControl . GetLogicalDrives ( ) . Count ( ) )
8381 {
8482 SetDiscCollection ( ) ;
8583
8684 return true ;
8785 }
88- else
89- return false ;
86+
87+ return false ;
9088 }
9189
9290 public static void UpdateSizeOfDiscs ( )
@@ -96,10 +94,17 @@ public static void UpdateSizeOfDiscs()
9694 foreach ( Device logic in LogicalDrives )
9795 {
9896 DriveInfo drive = new ( logic . Letter ) ;
99- logic . TotalSpace = drive . TotalSize ;
100- logic . FreeSpace = drive . TotalFreeSpace ;
101- logic . UsedSpace = drive . TotalSize - drive . AvailableFreeSpace ;
102- logic . PercentUsedSpace = Convert . ToByte ( logic . UsedSpace / ( logic . TotalSpace / 100 ) ) ;
97+ try
98+ {
99+ logic . TotalSpace = drive . TotalSize ;
100+ logic . FreeSpace = drive . TotalFreeSpace ;
101+ logic . UsedSpace = drive . TotalSize - drive . AvailableFreeSpace ;
102+ logic . PercentUsedSpace = Convert . ToByte ( logic . UsedSpace / ( logic . TotalSpace / 100 ) ) ;
103+ }
104+ catch ( DriveNotFoundException )
105+ {
106+ throw new DriveNotFoundException ( ) ;
107+ }
103108 }
104109 }
105110
@@ -109,7 +114,7 @@ public static void UpdateInfoOfDiscs()
109114
110115 foreach ( Device logical in LogicalDrives )
111116 {
112- SmartInfoCollection smart = DeviceIoList [ logical . Letter ] . Disc . Smart ;
117+ SmartInfoCollection smart = _deviceIoList [ logical . Letter ] . Disc . Smart ;
113118
114119 if ( smart != null )
115120 {
@@ -135,23 +140,23 @@ public static void UpdateInfoOfDiscs()
135140
136141 public static void PerfomanceDiskUpdate ( byte index )
137142 {
138- DiscApi . DISK_PERFORMANCE dISK_PERFORMANCE = Perfomances [ index ] . QueryPerformanceInfo ( ) ;
143+ DiscApi . DISK_PERFORMANCE dISK_PERFORMANCE = _perfomances [ index ] . QueryPerformanceInfo ( ) ;
139144
140- ReadIO = ( dISK_PERFORMANCE . BytesRead - PrevReadValues [ index ] ) / 1000000 ;
141- WriteIO = ( dISK_PERFORMANCE . BytesWritten - PrevWriteValues [ index ] ) / 1000000 ;
145+ ReadIO = ( dISK_PERFORMANCE . BytesRead - _prevReadValues [ index ] ) / 1000000 ;
146+ WriteIO = ( dISK_PERFORMANCE . BytesWritten - _prevWriteValues [ index ] ) / 1000000 ;
142147
143- PrevReadValues [ index ] = dISK_PERFORMANCE . BytesRead ;
144- PrevWriteValues [ index ] = dISK_PERFORMANCE . BytesWritten ;
148+ _prevReadValues [ index ] = dISK_PERFORMANCE . BytesRead ;
149+ _prevWriteValues [ index ] = dISK_PERFORMANCE . BytesWritten ;
145150 }
146151
147152 public static void Close ( )
148153 {
149- foreach ( Performance performance in Perfomances )
154+ foreach ( Performance performance in _perfomances )
150155 {
151156 performance ? . Dispose ( ) ;
152157 }
153158
154- foreach ( DeviceIoControl device in DeviceIoList . Values )
159+ foreach ( DeviceIoControl device in _deviceIoList . Values )
155160 {
156161 device . Dispose ( ) ;
157162 }
0 commit comments