本文介绍了我怎样才能在.NET中获取本机IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的VBScript code,返回的本地IP地址。它的伟大工程。我想提供相同的功能在我的winform .net应用程序。

所有我所遇到的解决方案涉及使用DNS。任何想法如何口这个剧本在.NET中使用?

一个不同的方式做到这一点,也许?

谢谢!

 功能GetIP()

 昏暗的WS:设置WS =的CreateObject(WScript.Shell的)
  昏暗FSO:设置FSO =的CreateObject(Scripting.FileSystemObject的)
  昏暗TMPFILE:TMPFILE = fso.GetSpecialFolder(2)及/ip.txt
  朦胧ThisLine,IP
  如果ws.Environment(SYSTEM)(OS)=然后,
    ws.run是winipcfg /批和放大器; TMPFILE,0,真
  其他
    ws.run%COMSPEC%/℃IPCONFIG>中和放大器; TMPFILE,0,真
  结束如果
  随着fso.GetFile(TMPFILE).OpenAsTextStream
    做虽然不.AtEndOfStream
      ThisLine = .ReadLine
      如果INSTR(ThisLine,地址)<> 0然后IP = MID(ThisLine,INSTR(ThisLine:)+ 2)
    循环
    。关闭
  结束与

  如果IP<>  然后
    如果升序(右(IP,1))= 13然后IP =左(IP,莱恩(IP) -  1)
  结束如果
  GetIP = IP
  fso.GetFile(TMPFILE).Delete
  设置FSO =什么
  设置WS =无
端功能
 

解决方案

您可以通过查询网络接口做到这一点,尽管这将包括所有本地地址,所以你可能需要添加一个其中,子句选择您想,如果有多个接口(其中有可能将是)一个。这当然不是你的脚本的直接端口,但希望能有点用处:

  VAR将localAddress =
    (来自镍的NetworkInterface.GetAllNetworkInterfaces()
     其中,ni.NetworkInterfaceType!= NetworkInterfaceType.Loopback
     让道具= ni.GetIPProperties()
     从ip地址在props.UnicastAddresses
     选择ip地址).FirstOrDefault();
 

请注意:如果您只需要IPv4地址,那么你可以修改查询:

  VAR将localAddress =
    (来自镍的NetworkInterface.GetAllNetworkInterfaces()
     其中,ni.NetworkInterfaceType!= NetworkInterfaceType.Loopback
     让道具= ni.GetIPProperties()
     从ip地址在props.UnicastAddresses
     其中,ipAddress.AddressFamily == AddressFamily.InterNetwork //的IPv4
     选择ip地址).FirstOrDefault();
 

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
解决方案

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();

这篇关于我怎样才能在.NET中获取本机IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:33