查找可用和空闲 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);
}
}
}