简体   繁体   English

C#控制台应用程序-在找到可用和可用的Ram空间时出现未处理的异常。在Windows窗体应用程序中获取确切答案

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

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... 找到可用和可用的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);
    }
}

} } 出现这样的错误。

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. 您有6个参数,仅填充其中5个。 Each parameter must have a corresponding value(0-based index for 5 values is 0-4). 每个参数必须具有一个对应的值(5个值的基于0的索引为0-4)。 See String.format and Console.WriteLine in MSDN for more info. 有关更多信息,请参见MSDN中的String.formatConsole.WriteLine

So that line should be: 因此,该行应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM