WPF 开发总结:
1、启用/禁用网络设备
通过调用win32 API hnetcfg.dll 具体索引路径为 :C:\Windows\System32,具体实现方法如下:
using NETCONLib;
/// <summary>
/// 禁用已连接的网络,重新启用连接
/// </summary>
public static void UpdateNetScan()
{
NetSharingManagerClass netSharingMgr = new NetSharingManagerClass();
INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
foreach (INetConnection connection in connections)
{
INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
INetSharingConfiguration netSharingConfiguration = netSharingMgr.get_INetSharingConfigurationForINetConnection(connection);
INetSharingPortMappingCollection objects = netSharingConfiguration.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_DEFAULT);
Console.WriteLine(connProps.MediaType.ToString());
string name = connProps.Name;
string DeviceName = connProps.DeviceName;
uint uinta = connProps.Characteristics;
if (DeviceName.Equals("以太网"))
if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN || connProps.MediaType == tagNETCON_MEDIATYPE.NCM_PPPOE)
{
try
{
if (connProps.Status == tagNETCON_STATUS.NCS_CONNECTED)
connection.Disconnect(); //禁用网络
if (connProps.Status == tagNETCON_STATUS.NCS_DISCONNECTED)
connection.Connect(); //启用网络
}
catch (Exception)
{
}
}
}
//UpdataNetScan2();
}
2、WPF 开发的软件,被远程桌面打开的一个问题:
通过远程登录运行WPF应用程序的电脑,UserControl控件的Loaded事件会重复触发:仅限Win10系统,Win7的不会;类似的事情,多个加载事件,也会发生在TabControl
中;
官方解释:当连接到远程会话时,操作系统会向每个窗口发送主题更改通知 (WM_THEMECHANGE)。Win7没有这样做,但Win10却这样做了。
解决方案:将loaded事件中的实现尽量放在初始化中;
官方回答过这个问题,可参考 https://github.com/dotnet/wpf/issues/4360;
https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.loaded?view=windowsdesktop-7.0&viewFallbackFrom=net-5.0#remarks
3、获取CPUID以及获取主板ID
///
/// 获取CPUID
///
///
///
- public static bool GetCpuID(string processorid)
- {
- System.Management.ManagementClass mc = new ManagementClass(“win32_processor”);
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- string cpuid = mo[“processorid”].ToString();
- if (cpuid.Equals(processorid)) return true;
- }
- return false;
- }
///
/// 获取主板ID
///
///
///
- public static bool GetSerialNumberID(string SerialNumber)
- {
- System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(“SELECT * FROM Win32_PhysicalMedia”);
- foreach (ManagementObject mo in searcher.Get())
- {
- string result = mo[“SerialNumber”].ToString().Trim();
- if (SerialNumber.Equals(result)) return true;
- }
- return false;
- }