|
11 | 11 |
|
12 | 12 | namespace NETworkManager.Models.Network |
13 | 13 | { |
14 | | - public class NetworkInterface |
| 14 | + public partial class NetworkInterface |
15 | 15 | { |
16 | 16 | #region Events |
17 | 17 | public event EventHandler UserHasCanceled; |
@@ -167,19 +167,18 @@ public static IPAddress DetectLocalIPAddressBasedOnRouting(IPAddress remoteIPAdd |
167 | 167 | { |
168 | 168 | bool isIPv4 = remoteIPAddress.AddressFamily == AddressFamily.InterNetwork; |
169 | 169 |
|
170 | | - using (var socket = new Socket(isIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) |
| 170 | + using var socket = new Socket(isIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); |
| 171 | + |
| 172 | + // return null on error... |
| 173 | + try |
171 | 174 | { |
172 | | - // return null on error... |
173 | | - try |
174 | | - { |
175 | | - socket.Bind(new IPEndPoint( isIPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0)); |
176 | | - socket.Connect(new IPEndPoint(remoteIPAddress, 0)); |
| 175 | + socket.Bind(new IPEndPoint(isIPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0)); |
| 176 | + socket.Connect(new IPEndPoint(remoteIPAddress, 0)); |
177 | 177 |
|
178 | | - if (socket.LocalEndPoint is IPEndPoint ipAddress) |
179 | | - return ipAddress.Address; |
180 | | - } |
181 | | - catch (SocketException) { } |
| 178 | + if (socket.LocalEndPoint is IPEndPoint ipAddress) |
| 179 | + return ipAddress.Address; |
182 | 180 | } |
| 181 | + catch (SocketException) { } |
183 | 182 |
|
184 | 183 | return null; |
185 | 184 | } |
@@ -249,72 +248,101 @@ public void ConfigureNetworkInterface(NetworkInterfaceConfig config) |
249 | 248 | } |
250 | 249 | } |
251 | 250 |
|
| 251 | + /// <summary> |
| 252 | + /// Flush the DNS cache asynchronously. |
| 253 | + /// </summary> |
| 254 | + /// <returns>Running task.</returns> |
252 | 255 | public static Task FlushDnsAsync() |
253 | 256 | { |
254 | 257 | return Task.Run(() => FlushDns()); |
255 | 258 | } |
256 | 259 |
|
| 260 | + /// <summary> |
| 261 | + /// Flush the DNS cache. |
| 262 | + /// </summary> |
257 | 263 | public static void FlushDns() |
258 | 264 | { |
259 | 265 | const string command = @"ipconfig /flushdns"; |
260 | 266 |
|
261 | 267 | PowerShellHelper.ExecuteCommand(command); |
262 | 268 | } |
263 | 269 |
|
264 | | - public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode) |
| 270 | + /// <summary> |
| 271 | + /// Release or renew the IP address of the specified network adapter asynchronously. |
| 272 | + /// </summary> |
| 273 | + /// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param> |
| 274 | + /// <param name="adapterName">Name of the ethernet adapter.</param> |
| 275 | + /// <returns>Running task.</returns> |
| 276 | + public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode, string adapterName) |
265 | 277 | { |
266 | | - return Task.Run(() => ReleaseRenew(mode)); |
| 278 | + return Task.Run(() => ReleaseRenew(mode, adapterName)); |
267 | 279 | } |
268 | 280 |
|
269 | | - public static void ReleaseRenew(IPConfigReleaseRenewMode mode) |
| 281 | + /// <summary> |
| 282 | + /// Release or renew the IP address of the specified network adapter. |
| 283 | + /// </summary> |
| 284 | + /// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param> |
| 285 | + /// <param name="adapterName">Name of the ethernet adapter.</param> |
| 286 | + public static void ReleaseRenew(IPConfigReleaseRenewMode mode, string adapterName) |
270 | 287 | { |
271 | | - if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Release) |
272 | | - { |
273 | | - const string command = @"ipconfig /release"; |
| 288 | + string command = string.Empty; |
274 | 289 |
|
275 | | - PowerShellHelper.ExecuteCommand(command); |
276 | | - } |
| 290 | + if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Release) |
| 291 | + command += @"ipconfig /release '" + adapterName + "';"; |
277 | 292 |
|
278 | 293 | if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Renew) |
279 | | - { |
280 | | - const string command = @"ipconfig /renew"; |
| 294 | + command += @"ipconfig /renew '" + adapterName + "';"; |
281 | 295 |
|
282 | | - PowerShellHelper.ExecuteCommand(command); |
283 | | - } |
| 296 | + if (mode == IPConfigReleaseRenewMode.ReleaseRenew6 || mode == IPConfigReleaseRenewMode.Release6) |
| 297 | + command += @"ipconfig /release6 '" + adapterName + "';"; |
| 298 | + |
| 299 | + if (mode == IPConfigReleaseRenewMode.ReleaseRenew6 || mode == IPConfigReleaseRenewMode.Renew6) |
| 300 | + command += @"ipconfig /renew6 '" + adapterName + "';"; |
| 301 | + |
| 302 | + PowerShellHelper.ExecuteCommand(command); |
284 | 303 | } |
285 | 304 |
|
| 305 | + /// <summary> |
| 306 | + /// Add an IP address to a network interface asynchronously. |
| 307 | + /// </summary> |
| 308 | + /// <param name="config">Ethernet adapter name, IP address and subnetmask.</param> |
| 309 | + /// <returns>Running task.</returns> |
286 | 310 | public static Task AddIPAddressToNetworkInterfaceAsync(NetworkInterfaceConfig config) |
287 | 311 | { |
288 | 312 | return Task.Run(() => AddIPAddressToNetworkInterface(config)); |
289 | 313 | } |
290 | 314 |
|
| 315 | + /// <summary> |
| 316 | + /// Add an IP address to a network interface. |
| 317 | + /// </summary> |
| 318 | + /// <param name="config">Ethernet adapter name, IP address and subnetmask.</param> |
291 | 319 | public static void AddIPAddressToNetworkInterface(NetworkInterfaceConfig config) |
292 | 320 | { |
293 | 321 | var command = @"netsh interface ipv4 add address '" + config.Name + @"' " + config.IPAddress + @" " + config.Subnetmask; |
294 | 322 |
|
295 | 323 | PowerShellHelper.ExecuteCommand(command, true); |
296 | 324 | } |
297 | 325 |
|
| 326 | + /// <summary> |
| 327 | + /// Remove an IP address from a network interface asynchronously. |
| 328 | + /// </summary> |
| 329 | + /// <param name="config">Ethernet adapter name, IP address</param> |
| 330 | + /// <returns>Running task.</returns> |
298 | 331 | public static Task RemoveIPAddressFromNetworkInterfaceAsync(NetworkInterfaceConfig config) |
299 | 332 | { |
300 | 333 | return Task.Run(() => RemoveIPAddressFromNetworkInterface(config)); |
301 | 334 | } |
302 | 335 |
|
| 336 | + /// <summary> |
| 337 | + /// Remove an IP address from a network interface. |
| 338 | + /// </summary> |
| 339 | + /// <param name="config">Ethernet adapter name, IP address</param> |
303 | 340 | public static void RemoveIPAddressFromNetworkInterface(NetworkInterfaceConfig config) |
304 | 341 | { |
305 | 342 | var command = @"netsh interface ipv4 delete address '" + config.Name + @"' " + config.IPAddress; |
306 | 343 |
|
307 | 344 | PowerShellHelper.ExecuteCommand(command, true); |
308 | 345 | } |
309 | 346 | #endregion |
310 | | - |
311 | | - #region Enum |
312 | | - public enum IPConfigReleaseRenewMode |
313 | | - { |
314 | | - ReleaseRenew, |
315 | | - Release, |
316 | | - Renew |
317 | | - } |
318 | | - #endregion |
319 | 347 | } |
320 | 348 | } |
0 commit comments