We help IT Professionals succeed at work.

Simplest Way to Get IP Address of Local PC in VB 2010

Terry Rogers
Terry Rogers asked
on
What is the most simplest and easiest way to find the primary local IP address of the primary local Network adapter in Visual Basic 2010?
Comment
Watch Question

Hi there,

you might want to check this article and convert the code to VB.

http://www.csharp-examples.net/local-ip/

--TheDoctor

Commented:

using System.Net;

IPAddress[] a = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress myip in a)
{
  MessageBox.Show(myip.ToString());
}

Open in new window

Terry RogersIT Technical Architect

Author

Commented:
Thanks for your comments. I am very new to VB 2010 so am still learning.

wizzie83, I copied your code to a button event as shown.

I get the following errors...

Error      2      'System.Net' is a namespace and cannot be used as an expression.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      4      15      WindowsApplication1
Error      1      'Using' must end with a matching 'End Using'.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      4      9      WindowsApplication1
Error      3      Character is not valid.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      4      25      WindowsApplication1
Error      7      Character is not valid.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      8      35      WindowsApplication1
Error      5      Comma, ')', or a valid expression continuation expected.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      6      20      WindowsApplication1
Error      4      Identifier expected.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      5      11      WindowsApplication1
Error      6      Syntax error.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      7      1      WindowsApplication1
Error      8      Syntax error.      C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb      9      1      WindowsApplication1
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        using System.Net;
IPAddress[] a = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress myip in a)
{
  MessageBox.Show(myip.ToString());
}
    End Sub
End Class

Open in new window

Commented:
its c# code, i try to create a vb example.
Commented:

Imports System.Net

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim a As IPAddress()
        Dim ip As IPAddress

        a = Dns.GetHostAddresses(Dns.GetHostName())
        For Each ip In a
            MsgBox(ip.ToString())
        Next

    End Sub
End Class

Open in new window

Terry RogersIT Technical Architect

Author

Commented:
Thanks. That all works now. :)