A:增加GUID获取网卡信息;增加数据库Models

This commit is contained in:
jimmy committed 2025-08-01 14:57:35 +08:00
1 parent 6f2418af7b
commit 2fb4de59d9
9 files changed
+246 -3

No files matched your search

@@ -16,6 +16,7 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.1-dev-02317" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.1-dev-00953" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageReference Include="System.Management" Version="10.0.0-preview.6.25358.103"/>
</ItemGroup>
</Project>
@@ -0,0 +1,59 @@
using System.Management;
using System;
namespace Common.Utils;
public class NetworkAdapterHelper
{
/// <summary>
/// 通过 GUID 获取网卡信息
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static NetworkInfo? GetNetworkInfoByGuid(string guid)
{
try
{
// 注意:WMI中的网卡ID格式,通常是网卡GUID,但带大括号和大小写等,处理一下格式
string formattedGuid = guid;
if (!guid.StartsWith("{"))
formattedGuid = "{" + guid + "}";
if (!guid.EndsWith("}"))
formattedGuid = formattedGuid + "}";
string query = $"SELECT * FROM Win32_NetworkAdapter WHERE GUID = '{formattedGuid}'";
using (var searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
return new NetworkInfo
{
Name = mo["Name"]?.ToString(),
Description = mo["Description"]?.ToString(),
MACAddress = mo["MACAddress"]?.ToString(),
NetConnectionID = mo["NetConnectionID"]?.ToString(),
Speed = mo["Speed"] != null ? Convert.ToInt64(mo["Speed"]) : 0,
Status = mo["Status"]?.ToString()
};
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error getting network info: " + ex.Message);
}
return null;
}
}
public class NetworkInfo
{
public string? Name { get; set; }
public string? Description { get; set; }
public string? MACAddress { get; set; }
public string? NetConnectionID { get; set; }
public long Speed { get; set; }
public string? Status { get; set; }
}