44using System . IO ;
55using DeviceIOControl ;
66using DeviceIOControl . Native ;
7- using DeviceIOControl . Disc ;
87using DeviceIOControl . Disc . Smart ;
98
109namespace Hardware . Windows . DeviceIoInfo
1110{
1211 public static class DeviceInfo
1312 {
14- public static float ReadIO { get ; private set ; }
15- public static float WriteIO { get ; private set ; }
13+ public static float ReadIo { get ; private set ; }
14+ public static float WriteIo { get ; private set ; }
1615
1716 public static List < Device > LogicalDrives { get ; }
18- private static Dictionary < string , DeviceIoControl > _deviceIoList ;
1917
20- private static Performance [ ] _perfomances ;
21- private static float [ ] _prevReadValues ;
22- private static float [ ] _prevWriteValues ;
23-
2418 static DeviceInfo ( )
2519 {
2620 LogicalDrives = new ( ) ;
27- _deviceIoList = new ( ) ;
2821 }
2922
3023 private static void SetDiscCollection ( )
3124 {
32- LogicalDrives . Clear ( ) ;
33- _deviceIoList . Clear ( ) ;
25+ Close ( ) ;
3426
3527 IEnumerable < LogicalDrive > logicalDevicesIo = DeviceIoControl . GetLogicalDrives ( ) ;
3628
29+ short devIndex = 0 ;
30+
3731 foreach ( LogicalDrive logical in logicalDevicesIo )
3832 {
3933 if ( logical . Type != WinApi . DRIVE . FIXED )
4034 continue ;
4135
42- Device device = new ( ) ;
43- device . Letter = logical . Name ;
36+ Device device = new ( )
37+ {
38+ Index = devIndex ,
39+ Letter = logical . Name ,
40+ DeviceControl = new DeviceIoControl ( logical . Name )
41+ } ;
42+ device . DevicePerformance = device . DeviceControl . Disc . GetDiscPerformance ( ) ;
4443
4544 LogicalDrives . Add ( device ) ;
46- _deviceIoList . Add ( device . Letter , new DeviceIoControl ( device . Letter ) ) ;
47- }
48-
49- byte index = 0 ;
50-
51- _prevReadValues = new float [ LogicalDrives . Count ] ;
52- _prevWriteValues = new float [ LogicalDrives . Count ] ;
53- _perfomances = new Performance [ LogicalDrives . Count ] ;
5445
46+ devIndex ++ ;
47+ }
48+
5549 foreach ( Device dev in LogicalDrives )
5650 {
57- var smart = _deviceIoList [ dev . Letter ] . Disc . Smart ;
51+ SmartInfoCollection smart = dev . DeviceControl . Disc . Smart ;
5852
59- if ( smart != null ) dev . Model = smart . SystemParams . ModelNumber . Trim ( ' ' ) ;
60- else dev . Model = new DriveInfo ( dev . Letter ) . VolumeLabel ;
53+ dev . Model = smart != null ? smart . SystemParams . ModelNumber . Trim ( ' ' ) : new DriveInfo ( dev . Letter ) . VolumeLabel ;
6154
6255 if ( dev . Model . Contains ( "samsung" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . Samsung ;
6356 else if ( dev . Model . Contains ( "kingston" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . Kingston ;
6457 else if ( dev . Model . Contains ( "wdc" , StringComparison . OrdinalIgnoreCase ) ) dev . Vendor = VendorEnum . WDC ;
6558 else dev . Vendor = VendorEnum . Other ;
6659
67- _perfomances [ index ] = _deviceIoList [ dev . Letter ] . Disc . GetDiscPerformance ( ) ;
68-
69- DiscApi . DISK_PERFORMANCE diskPerformance = _perfomances [ index ] . QueryPerformanceInfo ( ) ;
60+ DiscApi . DISK_PERFORMANCE diskPerformance = dev . DevicePerformance . QueryPerformanceInfo ( ) ;
7061
71- _prevReadValues [ index ] = diskPerformance . BytesRead ;
72- _prevWriteValues [ index ] = diskPerformance . BytesWritten ;
73-
74- index ++ ;
62+ dev . PrevReadIo = diskPerformance . BytesRead ;
63+ dev . PrevWriteIo = diskPerformance . BytesWritten ;
7564 }
7665 }
7766
@@ -90,7 +79,7 @@ public static bool IsUpdateDiscs()
9079 public static void UpdateSizeOfDiscs ( )
9180 {
9281 if ( LogicalDrives == null ) SetDiscCollection ( ) ;
93-
82+
9483 foreach ( Device logic in LogicalDrives )
9584 {
9685 DriveInfo drive = new ( logic . Letter ) ;
@@ -112,54 +101,50 @@ public static void UpdateInfoOfDiscs()
112101 {
113102 if ( LogicalDrives == null ) SetDiscCollection ( ) ;
114103
115- foreach ( Device logical in LogicalDrives )
104+ foreach ( Device dev in LogicalDrives )
116105 {
117- SmartInfoCollection smart = _deviceIoList [ logical . Letter ] . Disc . Smart ;
106+ SmartInfoCollection smart = dev . DeviceControl . Disc . Smart ;
107+
108+ if ( smart == null ) continue ;
109+
110+ DiscApi . DRIVEATTRIBUTE [ ] driveAttributes = smart . GetAttributes ( ) ;
118111
119- if ( smart != null )
112+ foreach ( DiscApi . DRIVEATTRIBUTE attribute in driveAttributes )
120113 {
121- DiscApi . DRIVEATTRIBUTE [ ] driveAttributes = smart . GetAttributes ( ) ;
114+ if ( dev . Vendor == VendorEnum . Samsung )
115+ {
116+ if ( attribute . bAttrID == 177 ) dev . Health = attribute . bAttrValue ;
117+ if ( attribute . bAttrID == 241 ) dev . TotalWrite = attribute . RawValue / ( float ) int . MaxValue ;
118+ }
122119
123- foreach ( DiscApi . DRIVEATTRIBUTE attribute in driveAttributes )
120+ if ( dev . Vendor == VendorEnum . Kingston )
124121 {
125- if ( logical . Vendor == VendorEnum . Samsung )
126- {
127- if ( attribute . bAttrID == 177 ) logical . Health = attribute . bAttrValue ;
128- if ( attribute . bAttrID == 241 ) logical . TotalWrite = attribute . RawValue / ( float ) int . MaxValue ;
129- }
130-
131- if ( logical . Vendor == VendorEnum . Kingston )
132- {
133- if ( attribute . bAttrID == 231 ) logical . Health = attribute . bAttrValue ;
134- if ( attribute . bAttrID == 241 ) logical . TotalWrite = attribute . RawValue / 1024f ;
135- }
122+ if ( attribute . bAttrID == 231 ) dev . Health = attribute . bAttrValue ;
123+ if ( attribute . bAttrID == 241 ) dev . TotalWrite = attribute . RawValue / 1024f ;
136124 }
137125 }
138126 }
139127 }
140128
141- public static void PerfomanceDiskUpdate ( byte index )
129+ public static void PerfomanceDiskUpdate ( short index )
142130 {
143- DiscApi . DISK_PERFORMANCE dISK_PERFORMANCE = _perfomances [ index ] . QueryPerformanceInfo ( ) ;
131+ DiscApi . DISK_PERFORMANCE dIskPerformance = LogicalDrives [ index ] . DevicePerformance . QueryPerformanceInfo ( ) ;
144132
145- ReadIO = ( dISK_PERFORMANCE . BytesRead - _prevReadValues [ index ] ) / 1000000 ;
146- WriteIO = ( dISK_PERFORMANCE . BytesWritten - _prevWriteValues [ index ] ) / 1000000 ;
133+ ReadIo = ( dIskPerformance . BytesRead - LogicalDrives [ index ] . PrevReadIo ) / 1000000 ;
134+ WriteIo = ( dIskPerformance . BytesWritten - LogicalDrives [ index ] . PrevWriteIo ) / 1000000 ;
147135
148- _prevReadValues [ index ] = dISK_PERFORMANCE . BytesRead ;
149- _prevWriteValues [ index ] = dISK_PERFORMANCE . BytesWritten ;
136+ LogicalDrives [ index ] . PrevReadIo = dIskPerformance . BytesRead ;
137+ LogicalDrives [ index ] . PrevWriteIo = dIskPerformance . BytesWritten ;
150138 }
151139
152140 public static void Close ( )
153141 {
154- foreach ( Performance performance in _perfomances )
155- {
156- performance ? . Dispose ( ) ;
157- }
158-
159- foreach ( DeviceIoControl device in _deviceIoList . Values )
142+ foreach ( Device dev in LogicalDrives )
160143 {
161- device . Dispose ( ) ;
144+ dev . Dispose ( ) ;
162145 }
146+
147+ LogicalDrives . Clear ( ) ;
163148 }
164149 }
165150}
0 commit comments