The input string was not in an incorrect format.

12
Hello, I'm a 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 which is not pointing nor an error compiling only when I give Start it says:

  An unhandled exception of type 'System.FormatException' occurred in   mscorlib.dll

     

Additional information: The input string was not   in an incorrect format.

and break in 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";
    }
    
asked by anonymous 12.11.2015 / 18:57

4 answers

11

The RAM () method returns a string that is not convertible to an integer.

Notice this line of this method:

return MemSize.ToString() + "MB";

If you change to:

return MemSize.ToString();

should work.

    
12.11.2015 / 19:01
4

If you do not want to change your RAM() , you can use an overload of TrimEnd that receives an array of char to get the end, which would be your "MB" like this:

var ram = Convert.ToInt32(Program.HardwareInfo.RAM().TrimEnd("MB".ToCharArray()));
    
12.11.2015 / 19:05
3

You are getting something that is long transforming into string to then transform it into int and play on a double variable. This is very crazy and does not make sense.

Change the return from RAM() to:

return MemSize;

Obviously the method will return a long and not a string .

And in calling this method use:

public string RAMU_TIE() {
    return (Program.HardwareInfo.RAM() - RAM_TIE()).ToString();
}

Much simpler, right?

Depending on where you are going to use this, ToString() is unnecessary (changing the method type, of course.

I would do the same in RAM_TIE() . And it would still change some other things. Among them is how to get those values, as I've already shown in another answer to the AP . Actually I do not understand why time picks up one way and time picks up another. I strongly advise you to use the way RAMU_TIE() is used.

public int RAM_TIE() {
    return ram.NextValue();
}

It was so simple that you may not even need the method. I would leave it if it had significant semantic meaning, but it does not seem to be the case, the name does not even say what this method is for. It is out of standard.

If you do the same schema in the RAM() method, it is probably no longer necessary, so RAMU_TIE() would only need this:

(ram.NextValue() - ramTie.NextValue()).ToString();

I let the AP exercise this. The code can be absurdly simpler than it currently is.

    
12.11.2015 / 20:06
1
  

Just to not delete the answer, what I replied is wrong, it is possible   yes store int in double .

I think the error is exactly on the line you pointed to

double ram = Convert.ToInt32(Program.HardwareInfo.RAM());

You are assigning a int to a variable double

    
12.11.2015 / 18:59