vb.net 显示主机名和 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14465854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:59:25  来源:igfitidea点击:

Displaying host-name and IP address

vb.nethostname

提问by CelticWhisper

I'm trying to create a tool for users on our network to report their PC hostnames and IP addresses to the IT department. For assistance, we have a warehouse department that loves to shuffle around PCs and users, so I can't reliably tell who's on what PC. This is intended to be a quick way to stop them from saying "Jim's PC" and get them to give me useful information like "WAREHOUSE_WINXP_4".

我正在尝试为我们网络上的用户创建一个工具,以向 IT 部门报告他们的 PC 主机名和 IP 地址。为了寻求帮助,我们有一个仓库部门,喜欢在 PC 和用户之间混杂,所以我无法确定谁在哪台 PC 上。这是为了阻止他们说“Jim 的 PC”并让他们向我提供诸如“WAREHOUSE_WINXP_4”之类的有用信息的快速方法。

I've created what I think should be a working program, and while it compiles and executes without errors, I can't seem to get the text box contents to set properly. The program is super-simple--just a form with 2 text boxes.

我已经创建了我认为应该是一个工作程序,虽然它编译和执行没有错误,但我似乎无法正确设置文本框内容。该程序非常简单——只是一个带有 2 个文本框的表单。

(Note: I'm more of a sysadmin/netadmin/infosec specialist, so I'm probably making some id10t mistakes here, but I'm kinda out of ideas on my own.)

(注意:我更像是一个系统管理员/网络管理员/信息安全专家,所以我可能在这里犯了一些 id10t 错误,但我自己有点想法。)

Public Class Form1
    Dim strHostname As String
    Dim strIPAddress As String

    Public Sub getHostname()
        strHostname = System.Net.Dns.GetHostName()
        'txtHostname.Text = strHostname  Apparently putting it here won't work.  In Load() maybe?
    End Sub

    Public Sub getIPAddress()
        strIPAddress = System.Net.Dns.GetHostEntry(strHostname).AddressList(0).ToString()
        'txtIPAddress.Text = strIPAddress Apparently putting it here won't work.  In Load() maybe?
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        getIPAddress()
        getHostname()
        txtHostname.Text = strHostname
        txtIPAddress.Text = strIPAddress
    End Sub
End Class

回答by Steven Doggart

As others have mentioned, you are calling the methods in the wrong order. Your getIPAddressmethod uses the strHostnamevariable. The strHostnamevariable is set by the getHostnamemethod. Therefore, you need to call getHostnamefirst, before you can call getIPAddress.

正如其他人所提到的,您以错误的顺序调用方法。您的getIPAddress方法使用该strHostname变量。该strHostname变量由设置getHostname方法。因此,您需要先调用getHostname,然后才能调用getIPAddress

However, the real problem is that you have designed your code in a way where it is possibleto call the methods in the wrong order. It's always best to not have hidden rules like that which can easily cause bugs. For instance, if you wrote the code like this, the order in which you call the methods wouldn't matter anymore:

然而,真正的问题是您设计的代码可能以错误的顺序调用方法。最好不要有类似容易导致错误的隐藏规则。例如,如果您像这样编写代码,则调用方法的顺序将不再重要:

Imports System.Net

Public Class Form1
    Public Function GetHostName() As String
        Return Dns.GetHostName()
    End Sub

    Public Function GetIpAddress() As String
        Return Dns.GetHostEntry(GetHostname()).AddressList(0).ToString()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        txtHostname.Text = GetHostName()
        txtIPAddress.Text = GetIpAddress()
    End Sub
End Class

回答by maelstrom

You are setting the value of the global variable strHostnamein the call to getHostname, but you are using that variable in the call to getIPAddress. However, you are calling getIPAddressbeforeyou call getHostName. Reverse the order of the calls and it should work.

strHostname在对 的调用中设置全局变量的值getHostname,但在对 的调用中使用了该变量getIPAddress。但是,您在调用getIPAddress之前调用getHostName。颠倒调用的顺序,它应该可以工作。

Better yet, you might reconsider your use of global variables to prevent dependencies like this. A function parameter would work in a more easily debuggable manner. You could use System.Net.Dns.GetHostEntry(strHostname).AddressList(0).ToString()as a parameter to getIPAddress.

更好的是,您可能会重新考虑使用全局变量来防止这样的依赖关系。函数参数将以更易于调试的方式工作。您可以将其System.Net.Dns.GetHostEntry(strHostname).AddressList(0).ToString()用作getIPAddress.

回答by r91087

Change the order of your getIPAddressand getHostnamemethod calls. You are using the strHostnamevalue inside the getIPAddressmethod, but it hasn't been assigned a value yet.

更改您的getIPAddressgetHostname方法调用的顺序。您正在方法strHostname内部使用该值getIPAddress,但尚未为其分配值。