C#-查找机器的本地IP地址而不是VM的本地IP地址 [英] C# - Finding my machine's local IP Address and not the VM's

查看:209
本文介绍了C#-查找机器的本地IP地址而不是VM的本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机上安装了VirtualBox VM,因此有一个以太网适配器出现。我正在通过以下方式枚举机器的IP地址列表:

I have a VirtualBox VM installed on my machine and as such there is an ethernet adapter that appears for it. I'm enumerating through the list of my machine's IP address via the following:

       public string GetLocalIpAddress()
       {
            try
            {
                string strHostName = Dns.GetHostName();

                // Then using host name, get the IP address list..
                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

                foreach (IPAddress ip in ipEntry.AddressList)
                {
                    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        return string.Format("({0})", ip.ToString());
                    }
                }
            }
            catch(Exception e)
            {
                Global.ApplicationLog.AddApplicationLog(EnumAppEventTypes.SYSTEM_ERROR, e.ToString());
            }

            return "";
        }

我的问题是虚拟机的以太网适配器也会出现以下情况:

My problem is that the virtual machine's ethernet adapter also catches on the condition:

if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)

是否可以选择我的计算机的本地ip地址并忽略我的虚拟机?

Is there a way of picking out my machine's local ip address and disregarding my virtual machine's?

推荐答案

您可以忽略以太网适配器的名称。由于VM以太网适配器由有效的NIC驱动程序表示,因此从OS的角度来看,它完全等效于计算机的物理NIC。

You can disregard the Ethernet adapter by its name. As the VM Ethernet adapter is represented by a valid NIC driver, it is fully equivalent to the physical NIC of your machine from the OS's point of view.

这篇关于C#-查找机器的本地IP地址而不是VM的本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆