asp.Netマシンハードウェア情報(cpu周波数、ディスク空き容量、メモリ容量など)の取得
4919 ワード
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Win32;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;//( System.Management.dll System.Management )
namespace EC
{
///
/// (cpu 、 、 ……)
///
public class CpuInfoObject
{
public CpuInfoObject()
{
//
// TODO:
//
}
#region CPU
/**************************************************
* :GetCPUFrequency()
* : CPU
* :
* :
* Response.Write(EC.CpuInfoObject.GetCPUFrequency());
************************************************/
///
/// CPU
///
/// cpu
public static int GetCPUFrequency()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("~MHz");
int CPUFrequency = (int)obj;
return CPUFrequency;
}
#endregion
#region CPU
/**************************************************
* :GetCPUName()
* : CPU
* :
* :
* Response.Write(EC.CpuInfoObject.GetCPUName());
************************************************/
///
/// CPU
///
/// cpu
public static string GetCPUName()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
object obj = rk.GetValue("ProcessorNameString");
string CPUName = (string)obj;
return CPUName.TrimStart();
}
#endregion
#region
/**************************************************
* :GetFreeDiskSpace(string DiskName)
* :
* :DiskName: D: E:
* :
* Response.Write(EC.CpuInfoObject.GetFreeDiskSpace("D:"));
************************************************/
///
///
///
/// :D: E:
///
public static long GetFreeDiskSpace(string DiskName)
{
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + DiskName + "\"");
disk.Get();
string totalByte = disk["FreeSpace"].ToString();
long freeDiskSpaceMb = Convert.ToInt64(totalByte) / 1024 / 1024;
return freeDiskSpaceMb;
}
#endregion
#region
/**************************************************
* :GetOSName()
* :
* :
* :
* Response.Write(EC.CpuInfoObject.GetOSName());
************************************************/
///
///
///
///
public static string GetOSName()
{
string Rev = "";
System.OperatingSystem osInfo = System.Environment.OSVersion;
switch(osInfo.Platform)
{
//Platform is Windows 95, Windows 98,Windows 98 Second Edition, or Windows Me.
case System.PlatformID.Win32Windows:
switch(osInfo.Version.Major)
{
case 0:
Rev ="Windows 95";
break;
case 10:
if(osInfo.Version.Revision.ToString()=="2222A")
Rev ="Windows 98 Second Edition";
else
Rev ="Windows 98";
break;
case 90:
Rev="Windows Me";
break;
}
break;
//Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,or Windows XP.
case System.PlatformID.Win32NT:
switch (osInfo.Version.Major)
{
case 3:
Rev = "Windows NT 3.51";
break;
case 4:
Rev = "Windows NT 4.0";
break;
case 5:
if (osInfo.Version.Minor == 0)
Rev = "Windows 2000";
else if (osInfo.Version.Minor == 2)
Rev = "Windows 2003";
else
Rev = "Windows XP";
break;
}
break;
}
return Rev;
}
#endregion
}
}