DotNet中获取系统信息(一)

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

namespace  SystemInfomation
{
    
public struct RunnedTime
    
{
        
public int hour;
        
public int minute;
        
public int second;
    }

    
    
/**//// <summary>
    
/// Summary description for GetInfo.
    
/// </summary>

    public class SystemInfomation
    
{    
        
private static SystemInfomation instance;
        
private const string processorKey = @"HARDWARE\DESCRIPTION\System\CentralProcessor\";
        
private const string biosKey = @"HARDWARE\DESCRIPTION\System";
        
private const string networkKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\";
//        private static RegistryKey processor;
//        private static RegistryKey bios;
//        private static RegistryKey network;

        
/**//// <summary>
        
/// Get subkeys of processor registry.
        
/// </summary>
        
/// <returns>subKeys:stirng[]</returns>

        private string[] GetProcessorKey()
        
{
            
try
            
{
                RegistryKey processor 
= Registry.LocalMachine.OpenSubKey(processorKey);
                
return processor.GetSubKeyNames();
            }

            
catch (Exception e)
            
{
                
throw(e);
            }

        }


        
/**//// <summary>
        
/// Get bios registry.
        
/// </summary>
        
/// <returns>Registry</returns>

        private static RegistryKey GetBiosKey()
        
{
            
return Registry.LocalMachine.OpenSubKey(biosKey);
        }


        
/**//// <summary>
        
/// Get subkeys of netword card registry.
        
/// </summary>
        
/// <returns>subKeys:string[]</returns>

        private string[] GetNetworkKey()
        
{
            
try
            
{
                RegistryKey network 
= Registry.LocalMachine.OpenSubKey(networkKey);
                
return network.GetSubKeyNames();
            }

            
catch (Exception e)
            
{
                
throw(e);
            }

        }


        
public SystemInfomation()
        
{
            
//
            
// TODO: Add constructor logic here
            
//
        }


        
/**//// <summary>
        
/// Singleton Model
        
/// </summary>
        
/// <returns>instance:SystemInfo</returns>

        public SystemInfomation GetInstance()
        
{
            
lock (instance)
            
{
                
if (instance == null)
                
{
                    instance 
= new SystemInfomation();
                }

            }

            
return instance;
        }


        
/**//// <summary>
        
/// Get OS version.
        
/// </summary>
        
/// <returns>os_version:string</returns>

        public string GetOSVersion()
        
{
            
return System.Environment.OSVersion.ToString();
        }


        
/**//// <summary>
        
/// Get machine name
        
/// </summary>
        
/// <returns>machine_name:string</returns>

        public string GetMachineName()
        
{
            
return System.Environment.MachineName.ToString();
        }


        
/**//// <summary>
        
/// Get user name of using OS
        
/// </summary>
        
/// <returns>user_name:string</returns>

        public string GetUserName()
        
{
            
return System.Environment.UserName.ToString();
        }


        
/**//// <summary>
        
/// Get all logic drivers of machine
        
/// </summary>
        
/// <returns>logic_drivers:string[]</returns>

        public string[] GetLogicalDrives()
        
{
            
return System.Environment.GetLogicalDrives();
        }


        
/**//// <summary>
        
/// Get IP Address of machine
        
/// </summary>
        
/// <returns>ip_addr:IPAddress[]</returns>

        public IPAddress[] GetIPAddr()
        
{
            IPHostEntry ipEntry 
= Dns.GetHostByName(Dns.GetHostName());
            
return ipEntry.AddressList;
        }


        
/**//// <summary>
        
/// Get Domain name
        
/// </summary>

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