C# 控制台应用程序 - 在 windows 表单应用程序中找到可用和免费 Ram space.Getting 确切答案时出现未处理的异常

C# console application - Unhandled exception while finding the Available and free Ram space.Getting exact answer in windows forms application

在 windows 表单应用程序中找到可用和免费 Ram space.Getting 确切答案时出现未处理的异常,同时在文本框中显示详细信息但在控制台中出现异常 application.I 已添加所有可能参考..在编译时没有显示任何错误...

     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);
    }
}

}

现在您已经添加了所有代码,您的错误在这一行:

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

你有6个参数,只填其中的5个。每个参数必须有一个对应的值(5 个值的基于 0 的索引为 0-4)。 有关详细信息,请参阅 MSDN 中的 String.format and Console.WriteLine

所以那一行应该是:

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