WpfApp/README.md

139 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# # 配置文件读取
using System;
namespace WinFormsApp.Utils
{
/// <summary>
/// 配置读取示例类演示ConfigReader的各种用法
/// </summary>
public static class ConfigExample
{
/// <summary>
/// 数据库配置类
/// </summary>
public class DatabaseConfig
{
public string Name { get; set; } = "";
public bool Enabled { get; set; }
public int Type { get; set; }
public string Connection { get; set; } = "";
public string Remarks { get; set; } = "";
public bool Print { get; set; }
}
/// <summary>
/// 演示配置读取的各种方法
/// </summary>
public static void DemonstrateUsage()
{
Console.WriteLine("=== ConfigReader 使用示例 ===\n");
// 1. 读取字符串配置
string allowedHosts = ConfigReader.GetString("AllowedHosts", "localhost");
Console.WriteLine($"AllowedHosts: {allowedHosts}");
// 2. 读取嵌套字符串配置
string kestrelUrl = ConfigReader.GetString("Kestrel:Endpoints:Http:Url", "http://localhost:5000");
Console.WriteLine($"Kestrel URL: {kestrelUrl}");
// 3. 读取布尔配置
bool modulePattern = ConfigReader.GetBool("Module:Pattern", false);
Console.WriteLine($"Module Pattern: {modulePattern}");
// 4. 读取整数配置
int httpPort = ConfigReader.GetInt("HttpServer:Port", 8080);
Console.WriteLine($"HTTP Server Port: {httpPort}");
int logLevel = ConfigReader.GetInt("Logs:LogLevel", 1);
Console.WriteLine($"Log Level: {logLevel}");
// 5. 读取双精度浮点数配置
double maxFileSize = ConfigReader.GetDouble("Logs:MaxFileSize", 10.0);
Console.WriteLine($"Max File Size: {maxFileSize}MB");
// 6. 读取数组配置
string[] disabilities = ConfigReader.GetArray<string>("Module:Disability", new string[0]);
Console.WriteLine($"Module Disabilities: [{string.Join(", ", disabilities)}]");
// 7. 读取复杂对象配置(单个数据库配置)
DatabaseConfig[] dbConfigs = ConfigReader.GetObject<DatabaseConfig[]>("DB", new DatabaseConfig[0]);
Console.WriteLine($"\n数据库配置数量: {dbConfigs.Length}");
foreach (var config in dbConfigs)
{
Console.WriteLine($" - {config.Name}: Enabled={config.Enabled}, Type={config.Type}");
}
// 8. 使用泛型方法读取各种类型
var mqttPort = ConfigReader.GetValue<int>("MqttServer:Port", 1883);
var mqttUser = ConfigReader.GetValue<string>("MqttServer:User", "admin");
var mqttLog = ConfigReader.GetValue<bool>("MqttServer:Log", true);
Console.WriteLine($"\nMQTT配置:");
Console.WriteLine($" Port: {mqttPort}");
Console.WriteLine($" User: {mqttUser}");
Console.WriteLine($" Log: {mqttLog}");
// 9. 检查配置键是否存在
bool hasRedisConfig = ConfigReader.HasKey("Redis");
bool hasInvalidKey = ConfigReader.HasKey("InvalidKey");
Console.WriteLine($"\nRedis配置存在: {hasRedisConfig}");
Console.WriteLine($"无效键存在: {hasInvalidKey}");
// 10. 读取不存在的配置(返回默认值)
string nonExistentConfig = ConfigReader.GetString("NonExistent:Config", "默认值");
int nonExistentNumber = ConfigReader.GetInt("NonExistent:Number", 999);
Console.WriteLine($"\n不存在的配置:");
Console.WriteLine($" 字符串: {nonExistentConfig}");
Console.WriteLine($" 数字: {nonExistentNumber}");
Console.WriteLine("\n=== 示例结束 ===");
}
/// <summary>
/// 获取应用程序的主要配置信息
/// </summary>
/// <returns>配置信息字符串</returns>
public static string GetAppConfigSummary()
{
var summary = $@"
应用程序配置摘要:
- HTTP服务器端口: {ConfigReader.GetInt("HttpServer:Port", 8080)}
- WebSocket端口: {ConfigReader.GetInt("Websocket:Port", 3000)}
- MQTT服务器端口: {ConfigReader.GetInt("MqttServer:Port", 1883)}
- Redis启用状态: {ConfigReader.GetBool("Redis:En", false)}
- 模块默认路径: {ConfigReader.GetBool("Module:Pattern", true)}
- 日志级别: {ConfigReader.GetInt("Logs:LogLevel", 1)}
- 最大归档文件数: {ConfigReader.GetInt("Logs:MaxArchiveFiles", 50)}
";
return summary.Trim();
}
/// <summary>
/// 验证必要的配置项是否存在
/// </summary>
/// <returns>验证结果</returns>
public static bool ValidateRequiredConfigs()
{
string[] requiredKeys = {
"HttpServer:Port",
"Websocket:Port",
"MqttServer:Port",
"Module:Pattern",
"Logs:LogLevel"
};
bool allValid = true;
Console.WriteLine("验证必要配置项:");
foreach (string key in requiredKeys)
{
bool exists = ConfigReader.HasKey(key);
Console.WriteLine($" {key}: {(exists ? "✓" : "✗")}");
if (!exists) allValid = false;
}
return allValid;
}
}
}