55using Monbsoft . MachineMonitor . Views ;
66using System ;
77using System . Diagnostics ;
8+ using System . Linq ;
9+ using System . Management ;
810using System . Windows . Threading ;
911using static Monbsoft . MachineMonitor . Messages . UpdatedConfigurationMessage ;
1012
@@ -13,7 +15,7 @@ namespace Monbsoft.MachineMonitor.ViewModels
1315 public class MainViewModel : ViewModelBase
1416 {
1517 #region Champs
16- private const double Giga = 1073741824d ;
18+ private const double Mega = 1048576d ;
1719 private readonly ConfigurationStore _configuration ;
1820 private readonly PerformanceCounter _cpuCounter ;
1921 private readonly PerformanceCounter _memoryCounter ;
@@ -27,6 +29,7 @@ public class MainViewModel : ViewModelBase
2729 private double _networkMax ;
2830 private double _ram ;
2931 private MainWindow _view ;
32+ private double _memoryTotal ;
3033 #endregion
3134
3235 #region Constructeurs
@@ -37,8 +40,7 @@ public MainViewModel(ConfigurationStore configuration)
3740 {
3841 _configuration = configuration ;
3942 _cpuCounter = new PerformanceCounter ( "Processor" , "% Processor Time" , "_Total" ) ;
40- _memoryCounter = new PerformanceCounter ( "Memory" , "Available Bytes" ) ;
41- _totalRamCounter = new PerformanceCounter ( "Memory" , "Committed Bytes" ) ;
43+ _memoryCounter = new PerformanceCounter ( "Memory" , "Available MBytes" ) ;
4244
4345 if ( configuration != null && ! string . IsNullOrEmpty ( configuration . Network ) )
4446 {
@@ -103,7 +105,27 @@ public void Initialize(MainWindow view)
103105 {
104106 _view = view ;
105107 OnTransparencyChange ( _configuration . Transparent ) ;
108+
109+ // récupère le total de mémoire
110+ ManagementObjectSearcher searcher =
111+ new ManagementObjectSearcher (
112+ "SELECT * FROM Win32_ComputerSystem" ) ;
113+
114+ foreach ( var mobject in searcher . Get ( ) )
115+ {
116+ _memoryTotal = ToDouble ( mobject , "TotalPhysicalMemory" , Mega ) ;
117+ }
118+
106119 }
120+
121+ private static double ToDouble ( ManagementBaseObject mo , string name , double divider )
122+ {
123+ var value = mo [ name ] ;
124+ return value == null
125+ ? 0
126+ : ( double . TryParse ( value . ToString ( ) , out double result ) ? result / divider : 0 ) ;
127+ }
128+
107129 /// <summary>
108130 /// Starts the counters.
109131 /// </summary>
@@ -113,10 +135,10 @@ public void Start()
113135 }
114136 private double CalculatePercent ( )
115137 {
116- double total = _totalRamCounter . NextValue ( ) ;
117138 double free = _memoryCounter . NextValue ( ) ;
118- double use = ( total - free ) ;
119- return ( use / total ) * 100 ;
139+ double use = ( _memoryTotal - free ) ;
140+ double percent = Math . Round ( ( use / _memoryTotal ) * 100 , 2 ) ;
141+ return percent ;
120142 }
121143 private double GetPercentageNetwork ( )
122144 {
0 commit comments