11
Hello, I’m beginner in programming and I’m trying to show the user how much RAM is being consumed at the moment, I’m using the following code that is not pointing nor an error when compiling only when I give Start it says:
An unhandled Exception of type 'System.Formatexception' occurred in mscorlib.dll
Additional information: Input string was not in an incorrect format.
and break on this line:
double ram = Convert.ToInt32(Program.HardwareInfo.RAM());
Form1.Cs
PerformanceCounter ram = new PerformanceCounter("Memory", "Available MBytes", null);
public string RAM_TIE()
{
float ran = ram.NextValue();
int run = (int)ran;
return run.ToString();
}
public string RAMU_TIE()
{
int ramu = Convert.ToInt32(RAM_TIE());
double ram = Convert.ToInt32(Program.HardwareInfo.RAM());
double sub = ram - ramu;
return sub.ToString();
}
Program.Cs
public static string RAM()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();
long MemSize = 0;
long mCap = 0;
//
foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024) / 1024;
return MemSize.ToString() + "MB";
}
I decided to answer because I think this code is much simpler than it is.
– Maniero