使用C#连接wifi

需要安装库 Managed Native Wifi 获取Wifi列表 示例代码 private List<AvailableNetworkPack> GetAvaliableNetworks() { var networks = new List<AvailableNetworkPack>(); foreach (var network in NativeWifi.EnumerateAvailableNetworks()) { if (!networks.Contains(network)) { networks.Add(network); } } return networks; } 连接wifi 创建Profile 应该支持这些类型open, WEP and WPA-PSK的网络 private static string CreateSecurityWifiProfile(string ssid, string password) { string hex = CreateHexSSIDName(ssid); return string.Format(@"<?xml version=""1.0""?> <WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""> <name>{0}</name> <SSIDConfig> <SSID> <hex>{2}</hex> <name>{0}</name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>auto</connectionMode> <MSM> <security> <authEncryption> <authentication>WPA2PSK</authentication> <encryption>AES</encryption> <useOneX>false</useOneX> </authEncryption> <sharedKey> <keyType>passPhrase</keyType> <protected>false</protected> <keyMaterial>{1}</keyMaterial> </sharedKey> </security> </MSM> <MacRandomization xmlns=""http://www.microsoft.com/networking/WLAN/profile/v3""> <enableRandomization>false</enableRandomization> </MacRandomization> </WLANProfile>", ssid, password, hex); } private static string CreateOpenWifiProfile(string ssid) { string hex = CreateHexSSIDName(ssid); return string.Format(@"<?xml version=""1.0""?> <WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""> <name>{0}</name> <SSIDConfig> <SSID> <hex>{1}</hex> <name>{0}</name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>auto</connectionMode> <MSM> <security> <authEncryption> <authentication>open</authentication> <encryption>none</encryption> <useOneX>false</useOneX> </authEncryption> </security> </MSM> </WLANProfile>", ssid, hex); } private static string CreateHexSSIDName(string ssid) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ssid); return BitConverter.ToString(bytes).Replace("-", ""); } 连接到wifi var networks = GetAvaliableNetworks(); var network = networks.First(n => n.Ssid.ToString() == "gophers"); var profile = NativeWifi.EnumerateProfiles().FirstOrDefault(x => x.Name == "NTSP_CD"); if (profile is null) { var profileXml = CreateProfile("gophers", "gopherspwd"); // create a profile var profileResult = NativeWifi.SetProfile(network.Interface.Id, ProfileType.PerUser, profileXml, "AES", true); } // do connect var wasConnected = NativeWifi.ConnectNetwork(network.Interface.Id, network.Ssid.ToString(), network.BssType); 附录 一些常用的profile配置xml 摘自 项目https://github.com/DigiExam/simplewifi/ ...

2023-04-24 · 4 分钟 · czyt

.NET6 从JSON获取配置

环境准备 nuget包 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Binder Microsoft.Extensions.Configuration.Json (当需要从Json文件添加记录时,安装此nuget包) Microsoft.Extensions.Configuration.EnvironmentVariables (当需要从环境变量添加记录时,安装此nuget包) C#开发环境 visual studio 2019 + visual Code 示例代码 // See https://aka.ms/new-console-template for more information using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; Console.WriteLine("Hello, World!"); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); IConfiguration c = configurationBuilder.AddJsonFile("appsettings.json").AddEnvironmentVariables().Build(); var k = c.GetRequiredSection("Settings").Get<Settings>().KeyOne; var n = 1; public class NestedSettings { public string Message { get; set; } = null!; } public class Settings { public int KeyOne { get; set; } public bool KeyTwo { get; set; } public NestedSettings KeyThree { get; set; } = null!; } JSON 文件 (appsettings.json.ps:配置文件属性建议按下面属性设置) { "Settings": { "KeyOne": 1, "KeyTwo": true, "KeyThree": { "Message": "Oh, that's nice..." } } } 常见问题 如何自定义配置项目的Key 使用 [ConfigurationKeyName]属性进行设置,如下面的配置: public class ApiConfig { [ConfigurationKeyName("endpoint")] public string Endpoint { get; set; } } 配置热重载 配置热重载可以使用ChangeToken.OnChange来实现,参考微软官方文档 下面是一个例子: ...

2022-09-06 · 1 分钟 · czyt

WPF Prism 8如何注册Logging

Nuget包 基础包 Microsoft Logging Abstractions Microsoft Extensions DependencyInjection 可选日志包 可以按实际需求进行选择,如NLog等,我们这里采用的是 Serilog 这个Nuget包Serilog Extensions Logging 根据日志输出的目标不同,可以选择不同的扩展方法包 目标 包名 说明 文件 Serilog.Sinks.File WiteTo可以使用File方法详细说明 命令行 Serilog.Sinks.Console 调试输出 Serilog.Sinks.Debug WiteTo可以使用Debug方法 其他扩展,请搜索 点击 日志容器注册 我们使用的是 DryIoc 进行注册,需要安装Nuget包 DryIoc.Microsoft.DependencyInjection 具体代码如下: protected override IContainerExtension CreateContainerExtension() { var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)); return new DryIocContainerExtension(new Container(CreateContainerRules()) .WithDependencyInjectionAdapter(serviceCollection)); } 如果是Unity 则需要安装包 Unity.Microsoft.DependencyInjection 具体代码如下: protected override IContainerExtension CreateContainerExtension() { var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)); var container = new UnityContainer(); container.BuildServiceProvider(serviceCollection); return new UnityContainerExtension(container); } 我们对于不同的日志插件框架都是在 AddLogging这个地方进行添加的,需要注意的是我们不仅需要在ServiceCollection 中注册,还需要在容器对象中注册。注意区分 DryIoc中的WithDependencyInjectionAdapter 与Unity中的BuildServiceProvider使用差异。 ...

2022-03-08 · 1 分钟 · czyt