工作日记:C#获取操作系统、MAC地址、登录用户、网卡、物理内存信息

 /// <summary>  
        /// 操作系统的登录用户名  
        /// </summary>  
        /// <returns>系统的登录用户名</returns>  
        public static string GetUserName()
        {
            try
            {
                return Environment.UserName;
            //    var strUserName = string.Empty;
            //    var mc = new ManagementClass("Win32_ComputerSystem");
            //    var moc = mc.GetInstances();
            //    foreach (var mo in moc)
            //    {
            //        strUserName = mo["UserName"].ToString();
            //    }
            //    moc = null;
            //    mc = null;
            //    return strUserName;
            }
            catch
            {
                return "unknown";
            }
        }

        /// <summary>  
        /// 获取本机MAC地址  
        /// </summary>  
        /// <returns>本机MAC地址</returns>  
        public static string GetMacAddress()
        {
            try
            {
                var strMac = string.Empty;
                var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                var moc = mc.GetInstances();
                foreach (var mo in moc.Cast<ManagementBaseObject>().Where(mo => (bool)mo["IPEnabled"] == true))
                {
                    strMac = mo["MacAddress"].ToString();
                }
                moc = null;
                mc = null;
                return strMac;
            }
            catch
            {
                return "unknown";
            }
        }

        /// <summary>  
        /// 获取操作系统名称  
        /// </summary>  
        /// <returns>操作系统名称</returns>  
        public static string GetSystemName()
        {
            try
            {
                var strSystemName = string.Empty;
                var mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT PartComponent FROM Win32_SystemOperatingSystem");
                foreach (var mo in mos.Get())
                {
                    strSystemName = mo["PartComponent"].ToString();
                }
                mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT Caption FROM Win32_OperatingSystem");
                foreach (var mo in mos.Get())
                {
                    strSystemName = mo["Caption"].ToString();
                }
                mos = null;

                return strSystemName;
            }
            catch
            {
                return "unknown";
            }
        }

        /// <summary>  
        /// 获取物理内存信息  
        /// </summary>  
        /// <returns>物理内存信息</returns>  
        public static string GetTotalPhysicalMemory()
        {
            try
            {
                var strTotalPhysicalMemory = string.Empty;
                var mc = new ManagementClass("Win32_ComputerSystem");
                var moc = mc.GetInstances();
                foreach (var mo in moc)
                {
                    strTotalPhysicalMemory = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return strTotalPhysicalMemory;
            }
            catch
            {
                return "unknown";
            }
        }

        public static string GetNetworkIp()
        {
            try
            {
                var networkIp = new StringBuilder();
                var adapters = NetworkInterface.GetAllNetworkInterfaces(); //获取本机所有网卡对象
                foreach (var adapter in adapters)
                {
                    if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet && adapter.NetworkInterfaceType != NetworkInterfaceType.Ppp) continue;

                    if (networkIp.Length == 0)
                    {
                        networkIp.Append($"{adapter.Name }:");
                    }
                    else
                    {
                        networkIp.Append($";{adapter.Name }:");
                    }
                    var ipProperties = adapter.GetIPProperties(); //获取IP配置
                    var ipCollection = ipProperties.UnicastAddresses; //获取单播地址集
                    for (var i = 0; i < ipCollection.Count; i++)
                    {
                        var ip = ipCollection[i];
                        if (ip.Address.AddressFamily != AddressFamily.InterNetwork) continue;
                        if (i == ipCollection.Count - 1)
                        {
                            networkIp.Append(ip.Address);
                        }
                        else
                        {
                            networkIp.Append($"{ip.Address},");
                        }
                    }

                }
                return networkIp.Length == 0 ? "unknown" : networkIp.ToString();
            }
            catch (Exception)
            {
                return "unknown";
            }
        }

  

posted @ 2018-07-28 14:55  阿尔叶  阅读(505)  评论(0编辑  收藏  举报