using System.Text; using JackCraft.Config; namespace Test; internal static class Program { private static void Main(string[] args) { Console.WriteLine("Hello, World!"); var testClass = new TestClass { Test = "test", Abc = 123 }; // 示例1:使用默认的文件读取和保存方法 Console.WriteLine("\n=== 使用默认方法 ==="); var config1 = new Config(new FileInfo("./config1.json")); config1.Set("app.name", "MyApp"); config1.Set("app.version", "1.0.0"); Console.WriteLine($"App Name: {config1.Get("app.name")}"); Console.WriteLine($"App Version: {config1.Get("app.version")}"); // 示例2:使用自定义的读取和保存方法 Console.WriteLine("\n=== 使用自定义方法 ==="); // 自定义读取方法 - 添加日志 Config.ConfigReader customReader = filePath => { Console.WriteLine($"[自定义读取] 正在读取文件: {filePath}"); if (File.Exists(filePath)) { var content = File.ReadAllText(filePath); Console.WriteLine($"[自定义读取] 文件内容: {content}"); return content; } Console.WriteLine("[自定义读取] 文件不存在,返回空字符串"); return "{}"; }; // 自定义保存方法 - 添加日志和备份 Config.ConfigWriter customWriter = (filePath, content) => { Console.WriteLine($"[自定义保存] 正在保存到文件: {filePath}"); Console.WriteLine($"[自定义保存] 文件内容: {content}"); // 创建备份 if (File.Exists(filePath)) { var backupPath = filePath + ".backup"; File.Copy(filePath, backupPath, true); Console.WriteLine($"[自定义保存] 已创建备份: {backupPath}"); } File.WriteAllText(filePath, content); Console.WriteLine("[自定义保存] 文件保存完成"); }; var config2 = new Config(new FileInfo("./config2.json"), customReader, customWriter); config2.Set("app.name", "MyCustomApp"); config2.Set("app.version", "2.0.0"); config2.Set("app.author", "Jack"); Console.WriteLine($"App Name: {config2.Get("app.name")}"); Console.WriteLine($"App Version: {config2.Get("app.version")}"); Console.WriteLine($"App Author: {config2.Get("app.author")}"); // 示例3:使用加密的自定义读取和保存方法 Console.WriteLine("\n=== 使用加密的自定义方法 ==="); // 简单的Base64编码/解码作为加密示例 Config.ConfigReader encryptedReader = filePath => { Console.WriteLine($"[加密读取] 正在读取加密文件: {filePath}"); if (File.Exists(filePath)) { var encryptedContent = File.ReadAllText(filePath); try { var decodedBytes = Convert.FromBase64String(encryptedContent); var decodedContent = Encoding.UTF8.GetString(decodedBytes); Console.WriteLine("[加密读取] 解密成功"); return decodedContent; } catch { Console.WriteLine("[加密读取] 解密失败,返回空配置"); return "{}"; } } return "{}"; }; Config.ConfigWriter encryptedWriter = (filePath, content) => { Console.WriteLine($"[加密保存] 正在加密保存到文件: {filePath}"); var contentBytes = Encoding.UTF8.GetBytes(content); var encryptedContent = Convert.ToBase64String(contentBytes); File.WriteAllText(filePath, encryptedContent); Console.WriteLine("[加密保存] 加密保存完成"); }; var config3 = new Config(new FileInfo("./config3.encrypted"), encryptedReader, encryptedWriter); config3.Set("secret.apiKey", "12345-ABCDE"); config3.Set("secret.password", "mySecretPassword"); Console.WriteLine($"API Key: {config3.Get("secret.apiKey")}"); Console.WriteLine($"Password: {config3.Get("secret.password")}"); Console.WriteLine("\n所有示例执行完成!"); } } public class TestClass { public string? Test { get; set; } public int Abc { get; set; } }