C#中获取系统信息

 

取得当前系统信息的一系列方法,包括多网卡描述、IP地址等,适用于winform/wpf等本地应用程序。

 

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;
        }
    }
}


 

 

转载自:http://www.cnblogs.com/RIVERSPIRIT/articles/1350816.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值