使用C# WPF获取网络上所有已连接设备的操作系统名称可以通过以下步骤实现:
System.Net.NetworkInformation
命名空间下的NetworkInterface
类和IPAddress
类,通过遍历本地网络接口获取本地IP地址范围。System.Net.NetworkInformation.Ping
类,向每个IP地址发送Ping请求,通过判断是否有响应来确定设备是否在线。System.Management
命名空间下的ManagementObjectSearcher
和ManagementObject
类,利用WMI(Windows Management Instrumentation)查询操作系统相关信息。具体可以通过查询Win32_OperatingSystem
类的Caption
属性来获取操作系统名称。以下是一种实现方式的示例代码:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Management;
namespace GetConnectedDevicesOS
{
public class DeviceInfo
{
public string IpAddress { get; set; }
public string OsName { get; set; }
}
public class DeviceScanner
{
public List<DeviceInfo> GetConnectedDevicesOS()
{
List<DeviceInfo> connectedDevices = new List<DeviceInfo>();
// 获取本地网络的IP地址范围
List<IPAddress> localIpAddresses = GetLocalIpAddresses();
foreach (IPAddress localIpAddress in localIpAddresses)
{
// 扫描IP地址范围内的设备
List<IPAddress> connectedIpAddresses = GetConnectedIpAddresses(localIpAddress);
foreach (IPAddress connectedIpAddress in connectedIpAddresses)
{
// 获取在线设备的操作系统名称
string osName = GetDeviceOsName(connectedIpAddress);
// 构造设备信息对象
DeviceInfo deviceInfo = new DeviceInfo
{
IpAddress = connectedIpAddress.ToString(),
OsName = osName
};
connectedDevices.Add(deviceInfo);
}
}
return connectedDevices;
}
private List<IPAddress> GetLocalIpAddresses()
{
List<IPAddress> localIpAddresses = new List<IPAddress>();
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
foreach (IPAddressInformation ipInformation in ipProperties.UnicastAddresses)
{
if (ipInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIpAddresses.Add(ipInformation.Address);
}
}
}
}
return localIpAddresses;
}
private List<IPAddress> GetConnectedIpAddresses(IPAddress localIpAddress)
{
List<IPAddress> connectedIpAddresses = new List<IPAddress>();
Ping pingSender = new Ping();
string baseIpAddress = localIpAddress.ToString();
baseIpAddress = baseIpAddress.Substring(0, baseIpAddress.LastIndexOf('.') + 1);
for (int i = 1; i < 255; i++)
{
string ipAddress = baseIpAddress + i.ToString();
PingReply reply = pingSender.Send(ipAddress, 200);
if (reply.Status == IPStatus.Success)
{
connectedIpAddresses.Add(IPAddress.Parse(ipAddress));
}
}
return connectedIpAddresses;
}
private string GetDeviceOsName(IPAddress ipAddress)
{
string osName = string.Empty;
try
{
ConnectionOptions connectionOptions = new ConnectionOptions();
ManagementScope managementScope = new ManagementScope($"\\\\{ipAddress}\\root\\CIMv2", connectionOptions);
ObjectQuery objectQuery = new ObjectQuery("SELECT Caption FROM Win32_OperatingSystem");
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection)
{
osName = managementObject["Caption"].ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return osName;
}
}
}
这是一个简单的C# WPF应用程序,通过调用GetConnectedDevicesOS
方法可以获取所有已连接设备的IP地址和操作系统名称。注意,该示例代码中未涉及腾讯云的产品和链接地址,如需添加相关推荐,可以根据实际需求自行添加。
领取专属 10元无门槛券
手把手带您无忧上云