DotNet中获取系统信息(一)



前几天发了一张用C#获取系统信息的随笔,得到很多高人的指点,获益良多啊!
这两天又抽空重构了一下,使用了singleton模式,并支持读取多CPU和多网卡的信息(未使用WMI),但没办法测试,不知道行不行!请高手指导!
全过程都使用了TestDriver.NET(以前叫NUnitAddin)和ReSharper1.0,确实不错。尤其是ReSharper,重构功能比Togetherfor vs.net 2.0的好用多了,其它功能也不错(所以抛弃了VAX和CodeRush,多了太耗资源)。
代码较多,先发主要的:SystemInfomation.cs
using System;
using System.Net;
using Microsoft.Win32;

namespace SystemInfomation
{
publicstructRunnedTime
{
publicinthour;
publicintminute;
publicintsecond;
}


/**////<summary>
///SummarydescriptionforGetInfo.
///</summary>

publicclassSystemInfomation
{
privatestaticSystemInfomationinstance;
privateconststringprocessorKey=@"HARDWARE\DESCRIPTION\System\CentralProcessor\";
privateconststringbiosKey=@"HARDWARE\DESCRIPTION\System";
privateconststringnetworkKey=@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion\NetworkCards\";
//privatestaticRegistryKeyprocessor;
//privatestaticRegistryKeybios;
//privatestaticRegistryKeynetwork;

/**////<summary>
///Getsubkeysofprocessorregistry.
///</summary>
///<returns>subKeys:stirng[]</returns>

privatestring[]GetProcessorKey()
{
try
{
RegistryKeyprocessor
=Registry.LocalMachine.OpenSubKey(processorKey);
returnprocessor.GetSubKeyNames();
}

catch(Exceptione)
{
throw(e);
}

}


/**////<summary>
///Getbiosregistry.
///</summary>
///<returns>Registry</returns>

privatestaticRegistryKeyGetBiosKey()
{
returnRegistry.LocalMachine.OpenSubKey(biosKey);
}


/**////<summary>
///Getsubkeysofnetwordcardregistry.
///</summary>
///<returns>subKeys:string[]</returns>

privatestring[]GetNetworkKey()
{
try
{
RegistryKeynetwork
=Registry.LocalMachine.OpenSubKey(networkKey);
returnnetwork.GetSubKeyNames();
}

catch(Exceptione)
{
throw(e);
}

}


publicSystemInfomation()
{
//
//TODO:Addconstructorlogichere
//
}


/**////<summary>
///SingletonModel
///</summary>
///<returns>instance:SystemInfo</returns>

publicSystemInfomationGetInstance()
{
lock(instance)
{
if(instance==null)
{
instance
=newSystemInfomation();
}

}

returninstance;
}


/**////<summary>
///GetOSversion.
///</summary>
///<returns>os_version:string</returns>

publicstringGetOSVersion()
{
returnSystem.Environment.OSVersion.ToString();
}


/**////<summary>
///Getmachinename
///</summary>
///<returns>machine_name:string</returns>

publicstringGetMachineName()
{
returnSystem.Environment.MachineName.ToString();
}


/**////<summary>
///GetusernameofusingOS
///</summary>
///<returns>user_name:string</returns>

publicstringGetUserName()
{
returnSystem.Environment.UserName.ToString();
}


/**////<summary>
///Getalllogicdriversofmachine
///</summary>
///<returns>logic_drivers:string[]</returns>

publicstring[]GetLogicalDrives()
{
returnSystem.Environment.GetLogicalDrives();
}


/**////<summary>
///GetIPAddressofmachine
///</summary>
///<returns>ip_addr:IPAddress[]</returns>

publicIPAddress[]GetIPAddr()
{
IPHostEntryipEntry
=Dns.GetHostByName(Dns.GetHostName());
returnipEntry.AddressList;
}


/**////<summary>
///GetDomainname
///</summary>
///<returns>domain_name:stirng</returns>

publicstringGetDomainName()
{
returnSystem.Environment.UserDomainName.ToString();
}


/**////<summary>
///GetbootmodeofOS
///</summary>
///<returns>boot_mode:stirng</returns>

publicstringGetBootMode()
{
returnSystem.Windows.Forms.SystemInformation.BootMode.ToString();
}


/**////<summary>
///GetusedmemoryofOS
///</summary>
///<returns>used_memory:string</returns>

publicstringGetUsedMemory()
{
return(System.Environment.WorkingSet/(1024.0*1024.0)).ToString("F")+"M";
}


/**////<summary>
///Getsystemdirectory
///</summary>
///<returns>sys_dir:string</returns>

publicstringGetSystemDirectory()
{
returnSystem.Environment.SystemDirectory.ToString();
}


/**////<summary>
///Gettempdirectory
///</summary>
///<returns>tmp_dir:string</returns>

publicstringGetTempDirectory()
{
returnSystem.Environment.GetEnvironmentVariable("TEMP").ToString();
}


/**////<summary>
///Getdotnetframeworkversion
///</summary>
///<returns>fxversion:string</returns>

publicstringGetFxVersion()
{
returnSystem.Environment.Version.ToString();
}


/**////<summary>
///GetrunnedtimeofOS
///</summary>
///<returns><span s
分享到:
评论

你可能感兴趣的:(.net,windows,OS,Microsoft,F#)