C#获取本地计算机名,IP,MAC地址,硬盘ID

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;

/// <summary>
///Computer 的摘要说明
/// </summary>
public class Computer
{
    public string CpuID;
    public string MacAddress;
    public string DiskID;
    public string IpAddress;
    public string LoginUserName;
    public string ComputerName;
    public string SystemType;
    public string TotalPhysicalMemory; //单位:M
    private static Computer _instance;
    public static Computer Instance()
    {
        if (_instance == null)
            _instance = new Computer();
        return _instance;
    }
    public Computer()
    {
        CpuID = GetCpuID();
        MacAddress = GetMacAddress();
        DiskID = GetDiskID();
        IpAddress = GetIPAddress();
        LoginUserName = GetUserName();
        SystemType = GetSystemType();
        TotalPhysicalMemory = GetTotalPhysicalMemory();
        ComputerName = GetComputerName();
    }
    public string GetCpuID()
    {
        try
        {
            //获取CPU序列号代码
            string cpuInfo = "";//cpu序列号
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            moc = null;
            mc = null;
            return cpuInfo;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }
    public string GetMacAddress()
    {
        try
        {
            //获取网卡硬件地址
            string mac = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    mac = mo["MacAddress"].ToString();
                    break;
                }
            }
            moc = null;
            mc = null;
            return mac;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }
    public string GetIPAddress()
    {
        try
        {
            //获取IP地址
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    //st=mo["IpAddress"].ToString();
                    System.Array ar;
                    ar = (System.Array)(mo.Properties["IpAddress"].Value);
                    st = ar.GetValue(0).ToString();
                    break;
                }
            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }

    public string GetDiskID()
    {
        try
        {
            //获取硬盘ID
            String HDid = "";
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid = (string)mo.Properties["Model"].Value;
            }
            moc = null;
            mc = null;
            return HDid;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }

    /// <summary>
    /// 操作系统的登录用户名
    /// </summary>
    /// <returns></returns>
    public string GetUserName()
    {
        try
        {
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

                st = mo["UserName"].ToString();

            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }

    /// <summary>
    /// PC类型
    /// </summary>
    /// <returns></returns>
    public string GetSystemType()
    {
        try
        {
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

                st = mo["SystemType"].ToString();

            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }

    }

    /// <summary>
    /// 物理内存
    /// </summary>
    /// <returns></returns>
    public string GetTotalPhysicalMemory()
    {
        try
        {

            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

                st = mo["TotalPhysicalMemory"].ToString();

            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public string GetComputerName()
    {
        try
        {
            return System.Environment.GetEnvironmentVariable("ComputerName");
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }
    }

}

C#获取本地计算机名,IP,MAC地址,硬盘ID,布布扣,bubuko.com

时间: 2024-10-12 22:46:37

C#获取本地计算机名,IP,MAC地址,硬盘ID的相关文章

Java如何获取本地计算机的IP地址和主机名?

在Java编程中,如何获取本地计算机的IP地址和主机名? 以下示例显示如何使用InetAddress类的getLocalAddress()方法获取系统的本地IP地址和主机名. package com.yiibai; import java.net.InetAddress; public class GetLocalIPAddress { public static void main(String[] args) throws Exception { InetAddress addr = Ine

通过js获取计算机内网ip,计算机名,mac地址

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="

获取本地计算机名和IP地址

WSADATA wsadata; if(0 != WSAStartup(MAKEWORD(2, 2), &wsadata)) { AfxMessageBox("初始化网络环境失败!"); return TRUE; } char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); hostent *p = gethostbyname(szHostName); if(p == NULL) { AfxMessage

c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存

我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存. 首先需要引入命名空间: using System.Management; /// 操作系统的登录用户名: string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementCla

获取本地计算机的网络信息

Windows网络编程第六章探测网络中的在线设备 实验:获取本地计算机的网络信息 实验分析:主要练习对GetAdaptersInfo().GetNetworkParams().GetInterfaceInfo().GetIPAddrTable()等API函数的运用,实验中需要注意的是需要两次调用该函数,第一次返回本机要调查信息的数量,第二次调用显示具体信息. GetInfo.h void pAdapter(); void Network(); void NumberOfInterfaces();

获取Android设备WIFI的MAC地址 “MAC地址”

需要指出的是:wifi状态和wifi AP状态是互斥的状态:也就是一旦发现WIFI AP打开,WIFI是不能被打开的. 获取Android设备的WIFI MAC地址,首先需要将设备中的WIFI个人热点(AP)关闭:WIFI状态和WIFI AP状态是互斥的两种状态.也就是说:在WIFI AP打开的状态下,WIFI是不能被正常打开的. android系统获取MAC地址的多种方式遍历. 方法一:使用NetworkInterface 方法二: private static String getIpAnd

shell脚本批量获取内网主机的MAC地址

这个shell脚本用来批量获取192.168.4.0/24网段的内网主机的mac地址 #!/bin/bash #Author:Jink #这脚本使用ping和arp命令获取内网主机的MAC地址,查询到的MAC地址写入到文件中 myping(){ ping -c2 -w1 -i0.5 $1 &>/dev/null if [ $? -eq 0 ];then arp $1 | tail -1 | awk '{print $1,$3}'>> /tmp/mactable.txt fi }

获取局域网计算机名以及IP以及mac地址

void GetAll() { IDictionary<string, string> hostList = new Dictionary<string, string>(); DirectoryEntry MainGroup = new DirectoryEntry("WinNT:"); foreach (DirectoryEntry domain in MainGroup.Children) { foreach (DirectoryEntry pc in d

IP地址、计算机名称、MAC地址如何获取

以下的操作都在“命令提示窗口”中操作. 已知IP,如何获得计算机名称 方法(1): 使用ping -i ip地址 例如已知地址为192.168.1.168. 那么使用ping -i 192.168.1.168,如果能ping通该计算机.就可以获得该计算机的名称 方法(2): 使用nbtstat -a 加对方IP查对方的主机名 例如nbtstat -a 192.168.1.168 已知计算机名称,如何获得计算机的IP地址 方法(1): 使用ping 计算机名称 例如计算机名称是AAAA 那么使用p