使用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....

April 24, 2023 · 4 min · 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!...

September 6, 2022 · 1 min · 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....

March 8, 2022 · 1 min · czyt