.NET获取机器信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET获取机器信息相关的知识,希望对你有一定的参考价值。

     /// <summary>
        /// using System.Web;
        /// using System.Management;
        /// </summary>
        public static void main()
        {
            //客户端ip:
            HttpContext.Current.Request.ServerVariables.Get("Remote_Addr").ToString();
            //客户端主机名:
            HttpContext.Current.Request.ServerVariables.Get("Remote_Host").ToString();
            //客户端浏览器IE:
            string Browser = HttpContext.Current.Request.Browser.Browser;
            //客户端浏览器 版本号:
            int MajorVersion = System.Web.HttpContext.Current.Request.Browser.MajorVersion;
            //客户端操作系统:
            string Platform = System.Web.HttpContext.Current.Request.Browser.Platform;
            //服务器ip:
            HttpContext.Current.Request.ServerVariables.Get("Local_Addr").ToString();
            //端口:
            HttpContext.Current.Request.Url.Port.ToString();
            //服务器名:
            System.Web.HttpContext.Current.Request.ServerVariables.Get("Server_Name").ToString();

            //获取网卡的地址、本地的IP地址
            string stringMAC = "";
            string stringIP = "";
            ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection MOC = MC.GetInstances();
            foreach (ManagementObject MO in MOC)
            {
                if ((bool)MO["IPEnabled"] == true)
                {
                    stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
                    string[] IPAddresses = (string[])MO["IPAddress"]; //获取本地的IP地址
                    if (IPAddresses.Length > 0)
                    {
                        stringIP = IPAddresses[0];
                    }
                }
            }
            //获取远程客户端的网卡物理地址(MAC)(本机测试不了)
            string MAC = GetMac(HttpContext.Current.Request.UserHostAddress.ToString());

            //cpu序列号
            string cpuInfo = "";
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                string cpu = "cpu序列号:" + cpuInfo.ToString();
            }

            //获取硬盘ID
            String HDid;
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDid = (string)mo.Properties["Model"].Value;
                string yinpan = "硬盘序列号:" + HDid.ToString();
            }

            //获取网卡硬件地址
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    string yinpan = "MAC address/t{0}" + mo["MacAddress"].ToString();
                }
                mo.Dispose();
            }
        }

        /// <summary>
        /// 获取远程客户端的网卡物理地址(MAC)
        /// using System.Diagnostics;
        /// using System.Text.RegularExpressions;
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        private static string GetMac(string IP)   //para IP is the client‘s IP
        {
            string dirResults = "";
            ProcessStartInfo psi = new ProcessStartInfo();
            Process proc = new Process();
            psi.FileName = "nbtstat";
            psi.RedirectStandardInput = false;
            psi.RedirectStandardOutput = true;
            psi.Arguments = "-A " + IP;
            psi.UseShellExecute = false;
            proc = Process.Start(psi);
            dirResults = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();
            dirResults = dirResults.Replace("/r", "").Replace("/n", "").Replace("/t", "");

            Regex reg = new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Match mc = reg.Match(dirResults + "__MAC");

            if (mc.Success)
            {
                return mc.Groups["key"].Value;
            }
            else
            {
                reg = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                mc = reg.Match(dirResults);
                if (mc.Success)
                {
                    return "Host not found!";
                }
                else
                {
                    return "";
                }
            }
        }

 

以上是关于.NET获取机器信息的主要内容,如果未能解决你的问题,请参考以下文章

c# WMI获取机器硬件信息(硬盘,cpu,内存等)

错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段

如何在 discord.net 中获取用户、机器人和在线用户数?

HLS.js 获取视频片段信息

设备未收到 GCM 消息

java获取本机器的IP(linux和windows)

(c)2006-2024 SYSTEM All Rights Reserved IT常识