获取本地IP的四种方式

2025-09-04 05:52:07

1.第一种方式

采用System.Net.Dns的GetHostAddress的方式,具体请看代码:

1 ///

2 /// 网络不通畅可以获取

3 /// 不过能获取到具体的IP

4 ///

5 ///

6 public static List GetByGetHostAddresses()

7 {

8 try

9 {

10 IPAddress[] adds = Dns.GetHostAddresses(Dns.GetHostName());

11 return adds == null || adds.Length == 0 ? new List() : adds.ToList();

12 }

13 catch (Exception)

14 {

15 return new List();

16

17 }

18 }

这种方式受到网络的影响,如果没有连接到网络,本地配置的部分IP是获取不到的,我也遇到一种情况是,电脑环境正常,就是获取不到,原因至今还不知道;

2.第二种方式

采用 System.Management.ManagementClass来获取,详细请看代码:

1 ///

2 /// 只有网络通畅才能获取

3 ///

4 ///

5 public static List GetByManagementClass()

6 {

7 try

8 {

9 ManagementClass mClass = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");

10 ManagementObjectCollection managementObjectCollection = mClass.GetInstances();

11 List ls = new List();

12 foreach (var item in managementObjectCollection)

13 {

14 if ((bool)item["IPEnabled"] == true)

15 {

16 foreach (var ip in (string[])item["IPAddress"])

17 {

18 IPAddress ipout = null;

19 IPAddress.TryParse(ip, out ipout);

20 if (ipout != null)

21 {

22

23 ls.Add(ipout);

24 }

25 }

26 }

27 }

28 return ls;

29 }

30 catch (Exception)

31 {

32 return new List();

33

34 }

35 }

同样的这种方式也受到网络的约束,没有联网的状态下不一定能够获取到IP;

3.第三种方式

我们平时在命令行中输入ipconfig命令同样也是能获取,在程序中启动Ipconfig应用程序,然后解析出来,也是可以获取得到IP,详细请看代码:

1 public static List GetByCMD()

2 {

3 try

4 {

5 Process cmd = new Process();

6 cmd.StartInfo.FileName = "ipconfig.exe";

7 cmd.StartInfo.Arguments = "/all";

8 cmd.StartInfo.RedirectStandardOutput = true;

9 cmd.StartInfo.RedirectStandardInput = true;

10 cmd.StartInfo.UseShellExecute = false;

11 cmd.StartInfo.CreateNoWindow = true;

12 cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

13 cmd.Start();

14 string info = "";

15 List ls = new List();

16 // info = cmd.StandardOutput.ReadToEnd();

17 Regex validipregex = new Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}");

18 //new Regex(@"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");

19 while ((info = cmd.StandardOutput.ReadLine()) != null)

20 {

21 IPAddress ip = null;

22 Console.WriteLine(info);

23 info = validipregex.Match(info).Value;

24

25 IPAddress.TryParse(info, out ip);

26

27 if (ip != null)

28 {

29 ls.Add(ip);

30 }

31 }

32

33 cmd.WaitForExit();

34 cmd.Close();

35 return ls;

36 }

37 catch (Exception)

38 {

39 return new List();

40

41 }

42 }

即便是通过这种方式来获取IP,如果在本机电脑没有联网的状态下,也是获取不到IP的,并且也不太建议使用这种方式;

4.第四种方法

采用NetworkInterface.GetAllNetworkInterfaces的方式是不受网络的影响的,联网或者不联网都能够获取到IP,详细请看代码:

1 ///

2 /// 无论网络通不通都能获取到Ip

3 ///

4 ///

5 public static List GetByNetworkInterface()

6 {

7 try

8 {

9 NetworkInterface[] intf = NetworkInterface.GetAllNetworkInterfaces();

10 List ls = new List();

11 foreach (var item in intf)

12 {

13 IPInterfaceProperties adapterPropertis = item.GetIPProperties();

14 UnicastIPAddressInformationCollection coll = adapterPropertis.UnicastAddresses;

15 foreach (var col in coll)

16 {

17 ls.Add(col.Address);

18 }

19 }

20 return ls;

21 }

22 catch (Exception)

23 {

24 return new List();

25

26 }

27 }

以上所说的联网,包括连接在局域网中。

希望给有需要的朋友们带来帮助;

最新发表
友情链接