AdapterInfo.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Net.NetworkInformation;
  2. namespace NetworkTool.Client.Models;
  3. public sealed class AdapterInfo
  4. {
  5. public required string Id { get; set; }
  6. public required string Name { get; set; }
  7. public required string Description { get; set; }
  8. public required string Type { get; set; }
  9. public required bool IsUp { get; set; }
  10. public required bool HasLink { get; set; }
  11. public required int RecommendationScore { get; set; }
  12. public required string RecommendationLabel { get; set; }
  13. public required string RecommendationReason { get; set; }
  14. public string IPv4Address { get; set; } = string.Empty;
  15. public string IPv4Display => $"IPv4: {(string.IsNullOrWhiteSpace(IPv4Address) ? "无" : IPv4Address)}";
  16. public string DisplayName => $"{Name} | {IPv4Display}";
  17. public static AdapterInfo FromNetworkInterface(
  18. NetworkInterface adapter,
  19. string ipv4Address,
  20. int recommendationScore,
  21. string recommendationLabel,
  22. string recommendationReason)
  23. {
  24. return new AdapterInfo
  25. {
  26. Id = adapter.Id,
  27. Name = adapter.Name,
  28. Description = adapter.Description,
  29. Type = adapter.NetworkInterfaceType.ToString(),
  30. IsUp = adapter.OperationalStatus == OperationalStatus.Up,
  31. HasLink = adapter.OperationalStatus == OperationalStatus.Up,
  32. RecommendationScore = recommendationScore,
  33. RecommendationLabel = recommendationLabel,
  34. RecommendationReason = recommendationReason,
  35. IPv4Address = ipv4Address,
  36. };
  37. }
  38. }