如何使用 VB.NET 获取计算机的名称和 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2234757/
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
How do I get a computer's name and IP address using VB.NET?
提问by ahmed
How can i get ip address of system by sending mac ip address as input using vb.net coding?
如何通过使用 vb.net 编码发送 mac ip 地址作为输入来获取系统的 ip 地址?
回答by Shuwaiee
Use the My Class :)
使用我的课程 :)
My.Computer.Name
as for the IP address quick google search
至于IP地址快速谷歌搜索
Private Sub GetIPAddress()
Dim strHostName As String
Dim strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)
End Sub
回答by EagleEyes
Private Function GetIPv4Address() As String
GetIPv4Address = String.Empty
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
For Each ipheal As System.Net.IPAddress In iphe.AddressList
If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
GetIPv4Address = ipheal.ToString()
End If
Next
End Function
回答by Jayesh Sorathia
Here is Example for this. In this example we can get IP address of our given host name.
这是示例。在这个例子中,我们可以获得给定主机名的 IP 地址。
Dim strHostName As String = "jayeshsorathia.blogspot.com"
'string strHostName = "www.microsoft.com";
' Get DNS entry of specified host name
Dim addresses As IPAddress() = Dns.GetHostEntry(strHostName).AddressList
' The DNS entry may contains more than one IP addresses.
' Iterate them and display each along with the type of address (AddressFamily).
For Each address As IPAddress In addresses
Response.Write(String.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily))
Response.Write("<br/><br/>")
Next
回答by Gareth
Thanks Shuwaiee
谢谢 Shuwaiee
I made a slight change though as using it in a Private Sub
already.
尽管Private Sub
已经在使用它,但我做了一些细微的改变。
Dim GetIPAddress()
Dim strHostName As String
Dim strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)
But also made a change to the way the details are displayed so that they can show on seperate lines using & vbCrLf &
但也对细节的显示方式进行了更改,以便它们可以使用 & vbCrLf &
MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)
Hope this helps someone.
希望这可以帮助某人。
回答by TheProgrammer
Shows the Computer Name, Use a Button to call it
显示计算机名称,使用按钮调用它
Dim strHostName As String
strHostName = System.Net.Dns.GetHostName(). MsgBox(strHostName)
Dim strHostName As String
strHostName = System.Net.Dns.GetHostName(). MsgBox(strHostName)
Shows the User Name, Use a Button to call it
显示用户名,使用按钮调用它
If TypeOf My.User.CurrentPrincipal Is Security.Principal.WindowsPrincipal Then
Dim parts() As String = Split(My.User.Name, "\") Dim username As String = parts(1) MsgBox(username) End If
如果 TypeOf My.User.CurrentPrincipal 是 Security.Principal.WindowsPrincipal 那么
Dim parts() As String = Split(My.User.Name, "\") Dim username As String = parts(1) MsgBox(username) End If
For IP Address its little complicated, But I try to explain as much as I can. First write the next code, before Form1_Load but after import section
对于 IP 地址,它有点复杂,但我会尽可能多地解释。先写下一段代码,在 Form1_Load 之前但在 import 部分之后
Public Class Form1
Dim mem As String Private Sub GetIPAddress() Dim strHostName As String Dim strIPAddress As String strHostName = System.Net.Dns.GetHostName() strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString() mem = strIPAddress MessageBox.Show("IP Address: " & strIPAddress) End Sub
公开课表1
Dim mem As String Private Sub GetIPAddress() Dim strHostName As String Dim strIPAddress As String strHostName = System.Net.Dns.GetHostName() strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString() mem = strIPAddress MessageBox.Show("IP Address: " & strIPAddress) End Sub
Then in Form1_Load Section just call it
然后在 Form1_Load Section 中调用它
GetIPAddress()
获取IP地址()
Result: On form load it will show a msgbox along with the IP address, for put into Label1.text or some where else play with the code.
结果:在表单加载时,它会显示一个 msgbox 和 IP 地址,用于放入 Label1.text 或其他地方使用代码。
回答by JayBeeOH
Imports System.Net
Module MainLine
Sub Main()
Dim hostName As String = Dns.GetHostName
Console.WriteLine("Host Name : " & hostName & vbNewLine)
For Each address In Dns.GetHostEntry(hostName).AddressList()
Select Case Convert.ToInt32(address.AddressFamily)
Case 2
Console.WriteLine("IP Version 4 Address: " & address.ToString)
Case 23
Console.WriteLine("IP Version 6 Address: " & address.ToString)
End Select
Next
Console.ReadKey()
End Sub
End Module
回答by JayBeeOH
IP Version 4 Only ...
仅限 IP 版本 4 ...
Imports System.Net
Module MainLine
Sub Main()
Dim hostName As String = Dns.GetHostName
Console.WriteLine("Host Name: " & hostName & vbNewLine)
Console.WriteLine("IP Version 4 Address(es):")
For Each address In Dns.GetHostEntry(hostName).AddressList().
Where(Function(p) p.AddressFamily = Sockets.AddressFamily.InterNetwork)
Console.WriteLine(vbTab & address.ToString)
Next
Console.ReadKey()
End Sub
End Module
回答by AmanAgrawalTech
Label12.Text = "My ID : " + System.Net.Dns.GetHostByName(Net.Dns.GetHostName()).AddressList(0).ToString()
use this code, without any variable
使用此代码,没有任何变量
回答by user2663161
Dim ipAddress As IPAddress
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
ipAddress = ipHostInfo.AddressList(0)
回答by Jitendra Kumar
Public strHostName As String
Public strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)