Skip to content

Commit 7058281

Browse files
committed
Feature: Add network profile to networkinterfaceview
1 parent 271346e commit 7058281

7 files changed

Lines changed: 142 additions & 35 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
using NETworkManager.Localization.Resources;
5+
using NETworkManager.Models.Network;
6+
7+
namespace NETworkManager.Converters;
8+
9+
/// <summary>
10+
/// Convert <see cref="NetworkProfile" /> to a localized <see cref="string" /> or vice versa.
11+
/// </summary>
12+
public sealed class NetworkProfileToStringConverter : IValueConverter
13+
{
14+
/// <summary>
15+
/// Convert <see cref="NetworkProfile" /> to a localized <see cref="string" />.
16+
/// </summary>
17+
/// <param name="value">Object from type <see cref="NetworkProfile" />.</param>
18+
/// <param name="targetType"></param>
19+
/// <param name="parameter"></param>
20+
/// <param name="culture"></param>
21+
/// <returns>Localized <see cref="string" /> representing the network profile.</returns>
22+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
23+
{
24+
return value is not NetworkProfile profile
25+
? "-/-"
26+
: profile switch
27+
{
28+
NetworkProfile.Domain => Strings.Domain,
29+
NetworkProfile.Private => Strings.Private,
30+
NetworkProfile.Public => Strings.Public,
31+
_ => "-/-"
32+
};
33+
}
34+
35+
/// <summary>
36+
/// !!! Method not implemented !!!
37+
/// </summary>
38+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
}

Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/NETworkManager.Localization/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,6 +4043,9 @@ You can copy your profile files from “{0}” to “{1}” to migrate your exis
40434043
<data name="Block" xml:space="preserve">
40444044
<value>Block</value>
40454045
</data>
4046+
<data name="NetworkProfile" xml:space="preserve">
4047+
<value>Network profile</value>
4048+
</data>
40464049
<data name="NetworkProfiles" xml:space="preserve">
40474050
<value>Network profiles</value>
40484051
</data>

Source/NETworkManager.Models/Network/NetworkInterface.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using Microsoft.Win32;
1010
using NETworkManager.Utilities;
11+
using SMA = System.Management.Automation;
1112

1213
namespace NETworkManager.Models.Network;
1314

@@ -72,6 +73,37 @@ public static List<NetworkInterfaceInfo> GetNetworkInterfaces()
7273
{
7374
List<NetworkInterfaceInfo> listNetworkInterfaceInfo = [];
7475

76+
// Query network profiles (Domain/Private/Public) for all connected interfaces via PowerShell.
77+
// Keyed by InterfaceAlias which matches networkInterface.Name in the .NET API.
78+
var profileByAlias = new Dictionary<string, NetworkProfile>(StringComparer.OrdinalIgnoreCase);
79+
80+
try
81+
{
82+
using var ps = SMA.PowerShell.Create();
83+
ps.AddScript("Get-NetConnectionProfile | Select-Object InterfaceAlias, NetworkCategory");
84+
85+
foreach (var result in ps.Invoke())
86+
{
87+
var alias = result.Properties["InterfaceAlias"]?.Value?.ToString();
88+
var category = result.Properties["NetworkCategory"]?.Value?.ToString();
89+
90+
if (string.IsNullOrEmpty(alias))
91+
continue;
92+
93+
profileByAlias[alias] = category switch
94+
{
95+
"DomainAuthenticated" => NetworkProfile.Domain,
96+
"Private" => NetworkProfile.Private,
97+
"Public" => NetworkProfile.Public,
98+
_ => NetworkProfile.NotConfigured
99+
};
100+
}
101+
}
102+
catch
103+
{
104+
// Profile lookup is best-effort; proceed without profile information on error.
105+
}
106+
75107
foreach (var networkInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
76108
{
77109
// NetworkInterfaceType 53 is proprietary virtual/internal interface
@@ -194,7 +226,10 @@ public static List<NetworkInterfaceInfo> GetNetworkInterfaces()
194226
IPv6Gateway = [.. listIPv6Gateway],
195227
DNSAutoconfigurationEnabled = dnsAutoconfigurationEnabled,
196228
DNSSuffix = ipProperties.DnsSuffix,
197-
DNSServer = [.. ipProperties.DnsAddresses]
229+
DNSServer = [.. ipProperties.DnsAddresses],
230+
Profile = profileByAlias.TryGetValue(networkInterface.Name, out var profile)
231+
? profile
232+
: NetworkProfile.NotConfigured
198233
});
199234
}
200235

Source/NETworkManager.Models/Network/NetworkInterfaceInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public class NetworkInterfaceInfo
120120
public IPAddress[] DNSServer { get; set; }
121121

122122
/// <summary>
123-
/// Firewall network category (Private, Public, Domain)
123+
/// Network category assigned by Windows (Domain, Private, Public).
124+
/// <see cref="NetworkProfile.NotConfigured"/> when the interface has no active connection profile.
124125
/// </summary>
125-
// NOT IMPLEMENTED YET
126-
//public NetworkProfiles Profiles { get; set; }
126+
public NetworkProfile Profile { get; set; } = NetworkProfile.NotConfigured;
127127
}

Source/NETworkManager.Models/Network/NetworkProfiles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <summary>
44
/// Defines the network profile detected by Windows.
55
/// </summary>
6-
public enum NetworkProfiles
6+
public enum NetworkProfile
77
{
88
/// <summary>
99
/// Network profile is not configured.

0 commit comments

Comments
 (0)