DotNet中获取系统信息(一)

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

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for GetInfo.
ExpandedSubBlockEnd.gif    
/// </summary>

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get subkeys of processor registry.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>subKeys:stirng[]</returns>

InBlock.gif        private string[] GetProcessorKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RegistryKey processor 
= Registry.LocalMachine.OpenSubKey(processorKey);
InBlock.gif                
return processor.GetSubKeyNames();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get bios registry.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>Registry</returns>

InBlock.gif        private static RegistryKey GetBiosKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Registry.LocalMachine.OpenSubKey(biosKey);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get subkeys of netword card registry.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>subKeys:string[]</returns>

InBlock.gif        private string[] GetNetworkKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RegistryKey network 
= Registry.LocalMachine.OpenSubKey(networkKey);
InBlock.gif                
return network.GetSubKeyNames();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SystemInfomation()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Singleton Model
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>instance:SystemInfo</returns>

InBlock.gif        public SystemInfomation GetInstance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (instance)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    instance 
= new SystemInfomation();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return instance;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get OS version.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>os_version:string</returns>

InBlock.gif        public string GetOSVersion()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Environment.OSVersion.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get machine name
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>machine_name:string</returns>

InBlock.gif        public string GetMachineName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Environment.MachineName.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get user name of using OS
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>user_name:string</returns>

InBlock.gif        public string GetUserName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Environment.UserName.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get all logic drivers of machine
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>logic_drivers:string[]</returns>

InBlock.gif        public string[] GetLogicalDrives()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Environment.GetLogicalDrives();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get IP Address of machine
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>ip_addr:IPAddress[]</returns>

InBlock.gif        public IPAddress[] GetIPAddr()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IPHostEntry ipEntry 
= Dns.GetHostByName(Dns.GetHostName());
InBlock.gif            
return ipEntry.AddressList;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get Domain name
InBlock.gif        
/// </summary>
Image
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值