How to get local IP address in .NET?

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 application.

All the solutions I came across are related to using DNS. Any ideas on how to "port" this script for use in .net?

Is there any other way to do this?

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
+5
source share
5 answers

, , , where, , , (, , ). , , script, , , :

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

. IPv4, :

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();     
+9
using System.Net;

IPAddress localAddr = Dns.GetHostEntry(Dns.GetHostName().ToString()).AddressList[0];

[] hmmmm... , , DNS... ?

[] ....

, GetHostEntry,

IPAddress localAddr = Dns.GetHostEntry("").AddressList[0];
+5

, script ipconfig, . , - :

using System.Threading;

...

static void Main(string[] args)
{
    Process p = new Process();

    ProcessStartInfo psi = new ProcessStartInfo("ipconfig");
    psi.CreateNoWindow = true;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;

    p.StartInfo = psi;
    p.Start();

    string s = p.StandardOutput.ReadToEnd();
    int startPos = s.IndexOf(":", s.IndexOf("IPv4 Address"));
    string output = s.Substring(startPos + 2, s.IndexOf("\r\n", startPos) - startPos - 2);
    Console.WriteLine(output);
    Console.ReadLine();
}

, - , , , , script.

+2

DNS, , System.Management

string ipAddress = "";
ManagementObjectSearcher query =
    new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
    string[] addresses = (string[]) mo["IPAddress"];
    if (addresses.Length == 1 && addresses[0] != "0.0.0.0")
    {
        ipAddress = addresses[0];
        break;
    }

}
Console.WriteLine(ipAddress);

ip, .

+1

, IP-.

    String hostName = Dns.GetHostName();                      
    IPHostEntry local = Dns.GetHostByName(hostName);
    foreach (IPAddress ipaddress in local.AddressList)
    {
        ipaddress.ToString();  //this gives you the IP address
        //logic here 
    }
0

All Articles