Skip to content

Commit c2b192d

Browse files
authored
Merge pull request #2 from voltaney/fix/show-all-type-local-ip
Rename and enhance NetworkHelper methods
2 parents 0060e94 + 6c96066 commit c2b192d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

TouchSenderReceiver/Helpers/NetworkHelper.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.NetworkInformation;
1+
using System.Net;
2+
using System.Net.NetworkInformation;
23
using System.Net.Sockets;
34

45
namespace TouchSenderReceiver.Helpers;
@@ -11,7 +12,7 @@ public class NetworkHelper
1112
/// </summary>
1213
/// <param name="networkInterfaceType"></param>
1314
/// <returns></returns>
14-
public static IList<string> GetAllLocalIPv4(NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Ethernet)
15+
public static IList<string> GetAllLocalIPv4ByNIC(NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Ethernet)
1516
{
1617
List<string> ipAddrList = [];
1718
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
@@ -29,4 +30,21 @@ public static IList<string> GetAllLocalIPv4(NetworkInterfaceType networkInterfac
2930
}
3031
return ipAddrList;
3132
}
33+
34+
/// <summary>
35+
/// Get all local IPv4 addresses of the current machine.
36+
/// </summary>
37+
public static IEnumerable<string> GetAllLocalIPv4()
38+
{
39+
string hostname = Dns.GetHostName();
40+
41+
IPAddress[] adrList = Dns.GetHostAddresses(hostname);
42+
foreach (IPAddress address in adrList)
43+
{
44+
if (address.AddressFamily == AddressFamily.InterNetwork)
45+
{
46+
yield return address.ToString();
47+
}
48+
}
49+
}
3250
}

TouchSenderTablet.GUI/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public MainViewModel(ITouchReceiverSettingsService touchReceiverSettingsService,
112112
_serviceOptions = _touchReceiverSettingsService.ServiceOptions;
113113
_screenOptions = _touchReceiverSettingsService.ScreenOptions;
114114

115-
var candidateIpAddresses = NetworkHelper.GetAllLocalIPv4();
116-
IpAddresses = candidateIpAddresses.Count > 0 ? string.Join(" / ", NetworkHelper.GetAllLocalIPv4()) : "Unknown".GetLocalized();
115+
var candidateIpAddresses = NetworkHelper.GetAllLocalIPv4().ToList();
116+
IpAddresses = candidateIpAddresses.Count > 0 ? string.Join(" / ", candidateIpAddresses) : "Unknown".GetLocalized();
117117
}
118118

119119
private void SetInitialCanvas()

0 commit comments

Comments
 (0)