Unhandled exception while finding the Available and free Ram space.Getting exact answer in windows forms application while displaying details in a textbox but getting exception in console application.I've added all possible references..Not showing any error during compile time...

     using System;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Data;
     using System.Drawing;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using System.Diagnostics;
     using System.Management;
     using Microsoft.VisualBasic.Devices;
     using System.Runtime.Caching;
     using System.Net.Mail;
     using System.Net;
     using System.Runtime.InteropServices;
     using Outlook = Microsoft.Office.Interop.Outlook;
     using Office = Microsoft.Office.Core;
     using System.Reflection;
namespace Monitoring_Application
  {
class Program
{
    static void Main(string[] args)
    {
        sysmem();
        ram();


    }

    //SYSTEM MEMORY DETAILS 
    static void sysmem()
    {

        System.IO.DriveInfo sysmem1 = new System.IO.DriveInfo("c");
        System.IO.DriveInfo sysmem2 = new System.IO.DriveInfo("d");
        System.IO.DriveInfo sysmem3 = new System.IO.DriveInfo("e");
        string drivename = "Drive name : " + (sysmem1.Name).ToString();
        string drivetype = "Drive type : " + (sysmem1.DriveType).ToString();
        string driveformat ="Drive format : " + (sysmem1.DriveFormat).ToString();
        string max_space = "Maximum space (in GB) : " + ((sysmem1.TotalSize) / (float)1073741824).ToString("0.00");    //Calculates the max possible space in system
        string Available_space = "Available space (in GB) : " + ((sysmem1.AvailableFreeSpace) / (float)1073741824).ToString("0.00");  //calculates the total available free space in the system
        Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n",drivename,drivetype,driveformat,max_space,Available_space);
    }
    //SYSTEM RAM DETAILS
    static void ram()
    {
        PerformanceCounter cpuCounter;
        PerformanceCounter ramCounter;
        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";
        ramCounter = new PerformanceCounter("Memory", "Available MBytes");
        float temp = ramCounter.NextValue() / (1024);
        string max_space = "Maximum space (GB) : " + ((new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory) / (float)1073741824).ToString();
        string Available_space =  "Available space (GB) : " + temp.ToString();
        Console.WriteLine("{0}\n{1}", max_space, Available_space);
    }
}

} getting error like this....

1

There are 1 answers

0
JWiley On BEST ANSWER

Now that you've added all your code, your error is in this line:

Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n",drivename,drivetype,driveformat,max_space,Available_space);

You have 6 parameters, and only fill 5 of them. Each parameter must have a corresponding value(0-based index for 5 values is 0-4). See String.format and Console.WriteLine in MSDN for more info.

So that line should be:

Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}",drivename,drivetype,driveformat,max_space,Available_space);