获取 本机IP和Mac

/// <summary>
    /// 获取电脑名称
    /// </summary>
    /// <returns></returns>
    public static string GetUserName()
    {
        try
        {
            string strUserName = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                strUserName = mo["Name"].ToString();
            }
            moc = null;
            mc = null;
            return strUserName;
        }
        catch
        {
            return "unknown";
        }
    }
    /// <summary>
    /// 获取本机MAC地址
    /// </summary>
    /// <returns>本机MAC地址</returns>
    public static string GetMacAddress()
    {
        try
        {
            //'System.Management' 

            string strMac = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    strMac = mo["MacAddress"].ToString();
                }
            }
            moc = null;
            mc = null;
            return strMac;
        }
        catch
        {
            List<string> macList = GetMacByIPConfig();
            if (macList != null && macList.Count > 0)
            {
                return macList[0];
            }
            else
            {
                return "unknown";
            }
        }
    }


    ///<summary>
    /// 根据截取ipconfig /all命令的输出流获取网卡Mac
    ///</summary>
    ///<returns></returns>
    public static List<string> GetMacByIPConfig()
    {
        const string str4Search = "Physical Address. . . . . . . . . : ";
        const string str5Search = "物理地址. . . . . . . . . . . . . : ";

        List<string> macs = new List<string>();
        ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig", "/all");
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.CreateNoWindow = true;
        Process p = Process.Start(startInfo);
        //截取输出流
        StreamReader reader = p.StandardOutput;
        string line = reader.ReadLine();

        while (!reader.EndOfStream)
        {
            if (!string.IsNullOrEmpty(line))
            {
                line = line.Trim();

                if (line.StartsWith(str4Search))
                {
                    string mac = line.Substring(str4Search.Length);
                    mac = mac.Trim();
                    macs.Add(mac);
                }
                if (line.StartsWith(str5Search))
                {
                    string mac = line.Substring(str5Search.Length);
                    mac = mac.Trim();
                    macs.Add(mac);
                }
            }
            line = reader.ReadLine();
        }

        //等待程序执行完退出进程
        p.WaitForExit();
        p.Close();
        reader.Close();

        return macs;
    }

    /// <summary>
    /// 获取本机的IP地址
    /// </summary>
    /// <returns></returns>
    public static string GetLocalIP()
    {
        try
        {

            string HostName = Dns.GetHostName();
            IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
            for (int i = 0; i < IpEntry.AddressList.Length; i++)
            {
                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    string ip = "";
                    ip = IpEntry.AddressList[i].ToString();
                    return IpEntry.AddressList[i].ToString();
                }
            }
            return "";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值