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

 

posted @ 2016-09-07 16:48    阅读(747)  评论(0编辑  收藏  举报