WpfApp项目,使用依赖注入,用于开发桌面端软件
Go to file
2025-10-14 11:12:10 +08:00
Services 通过MainWindow传递xaml中的实例myChart1 2 3到ChartManager进行初始化图表,然后通过SensorChartManager中实时通过时间发布发哦ChartManager中 2025-10-14 11:08:00 +08:00
src 添加菜单控制器 2025-10-13 15:00:50 +08:00
Utils 剥离图表初始化和更新 2025-10-13 15:19:35 +08:00
.gitignore 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
App.xaml 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
App.xaml.cs UI进行中 2025-10-10 15:58:01 +08:00
appsettings.json 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
AssemblyInfo.cs 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
MainWindow.xaml 节点 2025-10-14 09:09:49 +08:00
MainWindow.xaml.cs 点击开始检测开始按钮逻辑内部之前清理所有图表中的数据 2025-10-14 11:12:10 +08:00
README.md 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
WpfApp.csproj 增加打开窗体 在底部通知栏显示相应的图标 2025-10-13 13:21:29 +08:00
WpfApp.csproj.user 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00
WpfApp.sln 项目初始化,完成日志服务,事件服务,数据库服务 2025-09-30 14:31:53 +08:00

# 配置文件读取

using System;

namespace WinFormsApp.Utils { ///

/// 配置读取示例类演示ConfigReader的各种用法 /// public static class ConfigExample { /// /// 数据库配置类 /// 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;
      }
    

    } }