C#获取机器信息(IPV4.IPV6.MAC.硬盘信息,机器厂商/型号)

把翻到的很多以前写的程序做个记录,记录学习过程,同时也方便以后查阅


https://github.com/Yiomo/GetInfo

该APP会获取部分机器信息并且在当前目录下生成一份TXT报告


1.引用

using System;
using System.Collections.Specialized;
using System.IO;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;


2.控件


省略一堆textblock一类的控件,能输出就行



3.获取机器名称

            string machineName = Environment.MachineName;
            bl1.Text = machineName;

4.获取MAC地址

            try
                {
                    using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                    {
                        using (ManagementObjectCollection moc = mc.GetInstances())
                        {
                            string macAddress = "";
                            foreach (ManagementObject mo in moc)
                            {
                                if ((bool)mo["IPEnabled"] == true)
                                {
                                    macAddress = mo["MacAddress"].ToString();
                                    break;
                                }
                            }
                        bl2.Text = macAddress;
                        }
                    }
                }
                catch
                {
                bl2.Text = "unknown";
                }
                finally
                {
                }


5.判断32/64位系统

            bool is64OS = Environment.Is64BitOperatingSystem;
            if (is64OS == true)
                bl3.Text = "64 bits OS";//64位
            else
                bl3.Text = "32 bits OS";//32位


6.获取硬盘信息

            string HDSN = "";
            ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDSN = (string)mo.Properties["Model"].Value;
            }
            ManagementClass mc1 = new ManagementClass("Win32_PhysicalMedia");
            ManagementObjectCollection moc2 = mc1.GetInstances();
            string HDID = "";
            foreach (ManagementObject mo in moc2)
            {
                HDID = mo.Properties["SerialNumber"].Value.ToString().Trim();
                break;
            }
            bl4.Text = HDSN;
            bl5.Text = HDID;



7.IPV4

        string[] GetLocalIpv4()
        {
            IPAddress[] localIPs;
            localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            StringCollection IpCollection = new StringCollection();
            foreach (IPAddress ip in localIPs)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)///AddressFamily.InterNetwork代表IPV4地址,不包含V6
                    IpCollection.Add(ip.ToString());
            }
            string[] IpArray = new string[IpCollection.Count];
            IpCollection.CopyTo(IpArray, 0);
            return IpArray;
        }


        void ShowIP()
        {
            foreach (string ip in GetLocalIpv4())
            {
                bl6.Text = ip.ToString();
            }
            return;
        }

        ShowIP();


8.IPV6

            string ip =  "";
            IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            for (int i = 0; i < addressList.Length; i++)
            {
                ip = addressList[i].ToString();
            }
            bl7.Text = ip;


9. 获取厂商和机器型号(manufacturer)(model)

            ManagementClass mc2 = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc3 = mc2.GetInstances();

            if (moc3.Count != 0)
            {
                foreach (ManagementObject mo in mc2.GetInstances())
                {
                    bl1.Text =mo["Manufacturer"].ToString();///制造厂商
                }
            }

            foreach (ManagementObject m in moc3)
            {
                PCV = m["model"].ToString ();
            }
            bl2.Text = PCV;机器型号


/*********************//



         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值