DotNet中获取系统信息

使用了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>
        /// <returns>domain_name:stirng</returns>
        public string GetDomainName()
        {
            return System.Environment.UserDomainName.ToString();
        }

        /**//// <summary>
        /// Get boot mode of OS
        /// </summary>
        /// <returns>boot_mode:stirng</returns>
        public string GetBootMode()
        {
            return System.Windows.Forms.SystemInformation.BootMode.ToString();
        }

        /**//// <summary>
        /// Get used memory of OS
        /// </summary>
        /// <returns>used_memory:string</returns>
        public string GetUsedMemory()
        {
            return (System.Environment.WorkingSet/(1024.0*1024.0)).ToString("F") + "M";
        }

        /**//// <summary>
        /// Get system directory
        /// </summary>
        /// <returns>sys_dir:string</returns>
        public string GetSystemDirectory()
        {
            return System.Environment.SystemDirectory.ToString();
        }

        /**//// <summary>
        /// Get temp directory
        /// </summary>
        /// <returns>tmp_dir:string</returns>
        public string GetTempDirectory()
        {
            return System.Environment.GetEnvironmentVariable("TEMP").ToString();
        }

        /**//// <summary>
        /// Get dotnet framework version
        /// </summary>
        /// <returns>fxversion:string</returns>
        public string GetFxVersion()
        {
            return System.Environment.Version.ToString();
        }

        /**//// <summary>
        /// Get runned time of OS
        /// </summary>
        /// <returns>RunnedTime struct</returns>
        public RunnedTime GetOSRunnedTime()
        {
            int t = System.Environment.TickCount/1000;
            int _hour = t/3600;
            int _minute = t/60 - _hour*60;
            int _second = t - _hour*3600 - _minute*60;

            RunnedTime time;
            time.hour = _hour;
            time.minute = _minute;
            time.second = _second;

            return time;
        }

        /**//// <summary>
        /// Get Processor's names
        /// </summary>
        /// <returns>processor_names:string[]</returns>
        public string[] GetProcessorName()
        {
            string[] processorNames;
            RegistryKey key = null;
            try
            {
                string[] subKeys = GetProcessorKey();
                processorNames = new string[subKeys.Length];

                for (int i = 0; i < subKeys.Length; i++)
                {
                    key = Registry.LocalMachine.OpenSubKey(processorKey + subKeys[i]);
                    processorNames[i] = key.GetValue("ProcessorNameString").ToString();
                }
            }
            catch (Exception e)
            {
                throw(e);
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
            return processorNames;
        }

        /**//// <summary>
        /// Get processor identifier
        /// </summary>
        /// <returns>processor_id:string[]</returns>
        public string[] GetProcessorIdentifier()
        {
            string[] processorId;
            RegistryKey key = null;
            try
            {
                string[] subKeys = GetProcessorKey();
                processorId = new string[subKeys.Length];

                for (int i = 0; i < subKeys.Length; i++)
                {
                    key = Registry.LocalMachine.OpenSubKey(processorKey + subKeys[i]);
                    processorId[i] = key.GetValue("Identifier").ToString();
                }
            }
            catch (Exception e)
            {
                throw(e);
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }

            return processorId;
        }

        /**//// <summary>
        /// Get vendor identifier
        /// </summary>
        /// <returns>vendor_id:string[]</returns>
        public string[] GetVendorIdentifier()
        {
            try
            {
                string[] subKeys = GetProcessorKey();
                string[] vendorId = null;
                RegistryKey key;

                for (int i = 0; i < subKeys.Length; i++)
                {
                    key = Registry.LocalMachine.OpenSubKey(processorKey + subKeys[i]);
                    vendorId[i] = key.GetValue("VendorIdentifier").ToString();
                }

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

        /**//// <summary>
        /// Get processor frequency(MHz)
        /// </summary>
        /// <returns>processor_freq:string[]</returns>
        public string[] GetProcessorFrequency()
        {
            string[] processorFrequency;
            RegistryKey key = null;
            try
            {
                string[] subKeys = GetProcessorKey();
                processorFrequency = new string[subKeys.Length];

                for (int i = 0; i < subKeys.Length; i++)
                {
                    key = Registry.LocalMachine.OpenSubKey(processorKey + subKeys[i]);
                    processorFrequency[i] = key.GetValue("~MHz").ToString() + " MHz";
                }   
            }
            catch (Exception e)
            {
                throw(e);
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
            return processorFrequency;
        }

        /**//// <summary>
        /// Get bios identifier
        /// </summary>
        /// <returns>bios_id:string</returns>
        public string GetBiosIdentifier()
        {
            return GetBiosKey().GetValue("Identifier").ToString();
        }

        /**//// <summary>
        /// Get bios version
        /// </summary>
        /// <returns>bios_version:string[]</returns>
        public string[] GetSystemBiosVersion()
        {
            return (string[])GetBiosKey().GetValue("SystemBiosVersion");
        }

        /**//// <summary>
        /// Get video biso version
        /// </summary>
        /// <returns>video_biso_version:string</returns>
        public string[] GetVideoBiosVersion()
        {
            return (string[])GetBiosKey().GetValue("VideoBiosVersion");
        }

        /**//// <summary>
        /// Get video bios date
        /// </summary>
        /// <returns>video_biso_date:string[]</returns>
        public string GetVideoBiosDate()
        {
            return GetBiosKey().GetValue("VideoBiosDate").ToString();
        }

        /**//// <summary>
        /// Get network cards description infomation
        /// </summary>
        /// <returns>network_card_info:string[]</returns>
        public string[] GetNetworkCardsInfo()
        {
            string[] networkInfo;
            RegistryKey key = null;
            try
            {
                string[] subKeys = GetNetworkKey();
                networkInfo = new string[subKeys.Length];

                for (int i = 0; i < subKeys.Length; i++)
                {
                    key = Registry.LocalMachine.OpenSubKey(networkKey + subKeys[i]);
                    networkInfo[i] = key.GetValue("Description").ToString();
                }
            }
            catch (Exception e)
            {
                throw(e);
            }
            finally
            {
                key.Close();
            }
            return networkInfo;
        }
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/johnsonTj/archive/2005/02/18/291820.aspx

原文地址:https://www.cnblogs.com/lifuyun/p/lifuyun09091823.html