Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get local IP in .NET?

Tags:

.net

vbscript

wmi

I have the following vbscript code that returns the local IP address. It works great. I am trying to provide the same functionality in my winform .net app.

All the solutions I have come across involve using DNS. Any ideas on how to "port" this script for use in .net?

A different way to do this maybe?

Thanks!

Function GetIP()

 Dim ws : Set ws = CreateObject("WScript.Shell")
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
  Dim ThisLine, IP
  If ws.Environment("SYSTEM")("OS") = "" Then
    ws.run "winipcfg /batch " & TmpFile, 0, True
  Else
    ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
  End If
  With fso.GetFile(TmpFile).OpenAsTextStream
    Do While NOT .AtEndOfStream
      ThisLine = .ReadLine
      If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
    Loop
    .Close
  End With

  If IP <> "" Then
    If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
  End If
  GetIP = IP
  fso.GetFile(TmpFile).Delete  
  Set fso = Nothing
  Set ws = Nothing
End Function
like image 607
Refracted Paladin Avatar asked Jun 05 '09 16:06

Refracted Paladin


People also ask

How do I find local IP addresses on my network?

How do I identify an unknown device on my network? To see all of the devices connected to your network, type arp -a in a Command Prompt window. This will show you the allocated IP addresses and the MAC addresses of all connected devices.

How do I find someone's local IP address?

Starting with the simplest way to find someone's IP address is to use one of the many IP lookup tools available online. Resources such as WhatIsMyIPAddress.com or WhatIsMyIP.com offer tools to enter an IP address and search for its free public registry results.


1 Answers

You can do this by querying the network interfaces, though this will include all local addresses so you may have to add a where clause to select the one you want if there are multiple interfaces (which there likely will be). It's certainly not a direct port of your script, but hopefully will be of some use:

var localAddress = 
    (from ni in NetworkInterface.GetAllNetworkInterfaces()
     where ni.NetworkInterfaceType != NetworkInterfaceType.Loopback
     let props = ni.GetIPProperties()
     from ipAddress in props.UnicastAddresses
     select ipAddress).FirstOrDefault();    

Note: If you only want IPv4 addresses, then you could alter the query to:

var localAddress = 
    (from ni in NetworkInterface.GetAllNetworkInterfaces()
     where ni.NetworkInterfaceType != NetworkInterfaceType.Loopback
     let props = ni.GetIPProperties()
     from ipAddress in props.UnicastAddresses
     where ipAddress.AddressFamily == AddressFamily.InterNetwork // IPv4
     select ipAddress).FirstOrDefault();     
like image 111
Greg Beech Avatar answered Sep 27 '22 22:09

Greg Beech