1

查找可用和空闲 RAM 空间时出现未处理的异常。在 Windows 窗体应用程序中获取准确答案,同时在文本框中显示详细信息但在控制台应用程序中获取异常。我添加了所有可能的引用..在编译期间未显示任何错误...

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

} 得到这样的错误....

4

1 回答 1

2

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

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.formatConsole.WriteLine

所以那行应该是:

Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}",drivename,drivetype,driveformat,max_space,Available_space);
于 2015-06-09T13:51:14.187 回答