MultiFormatDemo.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Apq.Cfg.Ini;
  2. using Apq.Cfg.Xml;
  3. using Apq.Cfg.Yaml;
  4. using Apq.Cfg.Toml;
  5. namespace Apq.Cfg.Samples.Demos;
  6. /// <summary>
  7. /// 示例 2: 多格式支持 - INI、XML、YAML、TOML
  8. /// </summary>
  9. public static class MultiFormatDemo
  10. {
  11. public static async Task RunAsync(string baseDir)
  12. {
  13. Console.WriteLine("═══════════════════════════════════════════════════════════════");
  14. Console.WriteLine("示例 2: 多格式支持 - INI、XML、YAML、TOML");
  15. Console.WriteLine("═══════════════════════════════════════════════════════════════\n");
  16. // INI 格式
  17. var iniPath = Path.Combine(baseDir, "config.ini");
  18. File.WriteAllText(iniPath, """
  19. [App]
  20. Name=IniApp
  21. Version=2.0.0
  22. [Database]
  23. Host=ini-server
  24. Port=5432
  25. """);
  26. // XML 格式
  27. var xmlPath = Path.Combine(baseDir, "config.xml");
  28. File.WriteAllText(xmlPath, """
  29. <?xml version="1.0" encoding="utf-8"?>
  30. <configuration>
  31. <App>
  32. <Name>XmlApp</Name>
  33. <Version>3.0.0</Version>
  34. </App>
  35. <Database>
  36. <Host>xml-server</Host>
  37. <Port>1433</Port>
  38. </Database>
  39. </configuration>
  40. """);
  41. // YAML 格式
  42. var yamlPath = Path.Combine(baseDir, "config.yaml");
  43. File.WriteAllText(yamlPath, """
  44. App:
  45. Name: YamlApp
  46. Version: 4.0.0
  47. Database:
  48. Host: yaml-server
  49. Port: 27017
  50. """);
  51. // TOML 格式
  52. var tomlPath = Path.Combine(baseDir, "config.toml");
  53. File.WriteAllText(tomlPath, """
  54. [App]
  55. Name = "TomlApp"
  56. Version = "5.0.0"
  57. [Database]
  58. Host = "toml-server"
  59. Port = 6379
  60. """);
  61. // 分别测试各格式
  62. Console.WriteLine("2.1 INI 格式:");
  63. using (var iniCfg = new CfgBuilder().AddIni(iniPath, level: 0, writeable: true).Build())
  64. {
  65. Console.WriteLine($" App:Name = {iniCfg.Get("App:Name")}");
  66. Console.WriteLine($" Database:Port = {iniCfg.Get("Database:Port")}");
  67. }
  68. Console.WriteLine("\n2.2 XML 格式:");
  69. using (var xmlCfg = new CfgBuilder().AddXml(xmlPath, level: 0, writeable: true).Build())
  70. {
  71. Console.WriteLine($" App:Name = {xmlCfg.Get("App:Name")}");
  72. Console.WriteLine($" Database:Port = {xmlCfg.Get("Database:Port")}");
  73. }
  74. Console.WriteLine("\n2.3 YAML 格式:");
  75. using (var yamlCfg = new CfgBuilder().AddYaml(yamlPath, level: 0, writeable: true).Build())
  76. {
  77. Console.WriteLine($" App:Name = {yamlCfg.Get("App:Name")}");
  78. Console.WriteLine($" Database:Port = {yamlCfg.Get("Database:Port")}");
  79. }
  80. Console.WriteLine("\n2.4 TOML 格式:");
  81. using (var tomlCfg = new CfgBuilder().AddToml(tomlPath, level: 0, writeable: true).Build())
  82. {
  83. Console.WriteLine($" App:Name = {tomlCfg.Get("App:Name")}");
  84. Console.WriteLine($" Database:Port = {tomlCfg.Get("Database:Port")}");
  85. }
  86. // 混合多种格式
  87. Console.WriteLine("\n2.5 混合多种格式(层级覆盖):");
  88. using var mixedCfg = new CfgBuilder()
  89. .AddIni(iniPath, level: 0, writeable: false)
  90. .AddYaml(yamlPath, level: 1, writeable: false)
  91. .AddToml(tomlPath, level: 2, writeable: true, isPrimaryWriter: true)
  92. .Build();
  93. Console.WriteLine($" App:Name = {mixedCfg.Get("App:Name")} (来自 TOML,最高优先级)");
  94. Console.WriteLine($" App:Version = {mixedCfg.Get("App:Version")} (来自 TOML)");
  95. File.Delete(iniPath);
  96. File.Delete(xmlPath);
  97. File.Delete(yamlPath);
  98. File.Delete(tomlPath);
  99. Console.WriteLine("\n[示例 2 完成]\n");
  100. await Task.CompletedTask;
  101. }
  102. }