Compare commits

..

3 Commits

Author SHA1 Message Date
bfbc7ba027 chore(build): 将解决方案文件从 .sln 格式迁移到 .slnx 格式
- 移除旧的 JackCraft.Config.sln 文件
- 添加新的 JackCraft.Config.slnx 解决方案文件
- 保留 JackCraft.Config 和 Test 项目配置
- 维持 LICENSE 和 Readme.MD 文件引用
- 更新解决方案文件格式以支持新版本 Visual Studio
2026-01-18 21:38:51 +08:00
839996e0d7 chore(config): 更新项目配置文件忽略设置
- 在 .gitignore 中添加 *.sln.DotSettings.user 忽略规则
- 移除旧的解决方案用户设置文件 JackCraft.Config.sln.DotSettings.user
2026-01-18 21:37:44 +08:00
f146150faa feat(config): 添加自定义读取和保存方法支持
- 引入 ConfigReader 和 ConfigWriter 委托类型定义
- 在 Config 构造函数中添加自定义读取和保存方法参数
- 实现 ReadConfigFile 和 WriteConfigFile 辅助方法
- 修改文件操作使用自定义方法或默认方法
- 更新测试程序添加多种自定义方法使用示例
- 在文档中添加自定义方法使用的代码示例
2026-01-18 21:36:24 +08:00
7 changed files with 171 additions and 53 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ obj/
riderModule.iml riderModule.iml
/_ReSharper.Caches/ /_ReSharper.Caches/
.idea/ .idea/
*.sln.DotSettings.user

View File

@@ -1,28 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JackCraft.Config", "JackCraft.Config\JackCraft.Config.csproj", "{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Folder", "Solution Folder", "{1D7EF989-92C0-4831-BD2D-995DC966C6D0}"
ProjectSection(SolutionItems) = preProject
LICENSE = LICENSE
Readme.MD = Readme.MD
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.Build.0 = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -1,7 +0,0 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACastHelpers_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003F54_003Ff2c78e92_003FCastHelpers_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AExceptionDispatchInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fbf9021a960b74107a7e141aa06bc9d8a0a53c929178c2fb95b1597be8af8dc_003FExceptionDispatchInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2610c78ec589e1aacd82c4428835c427b838e8156a6f346f517d28ef9ffbb9a2_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFile_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003Fa1_003F11a17405_003FFile_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003F90_003F0d01fe57_003FJsonSerializerInternalReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F29ea32a76b2a6b8eb246f247df6a6a86d887384b4c31db2a35effccdd86c24_003FJsonSerializerInternalReader_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

8
JackCraft.Config.slnx Normal file
View File

@@ -0,0 +1,8 @@
<Solution>
<Folder Name="/Solution Folder/">
<File Path="LICENSE" />
<File Path="Readme.MD" />
</Folder>
<Project Path="JackCraft.Config/JackCraft.Config.csproj" />
<Project Path="Test/Test.csproj" />
</Solution>

View File

@@ -5,21 +5,25 @@ namespace JackCraft.Config;
public class Config public class Config
{ {
public delegate string ConfigReader(string filePath);
public delegate void ConfigWriter(string filePath, string content);
private readonly FileInfo _configFile; private readonly FileInfo _configFile;
public readonly string ConfigFileName; private readonly ConfigReader? _customReader;
public readonly string ConfigFilePath; private readonly ConfigWriter? _customWriter;
private Dictionary<string, object> _configData = new(); private Dictionary<string, object> _configData = new();
public Config(FileInfo configFile) public Config(FileInfo configFile, ConfigReader? customReader = null, ConfigWriter? customWriter = null)
{ {
_configFile = configFile; _configFile = configFile;
ConfigFilePath = configFile.DirectoryName ?? string.Empty; _customReader = customReader;
ConfigFileName = configFile.Name; _customWriter = customWriter;
if (!configFile.Exists) if (!configFile.Exists)
{ {
configFile.Directory?.Create(); configFile.Directory?.Create();
using var stream = configFile.Create(); WriteConfigFile(configFile.FullName, "{}");
File.WriteAllText(configFile.FullName, "{}");
} }
LoadConfigData(); LoadConfigData();
@@ -29,7 +33,7 @@ public class Config
{ {
if (File.Exists(_configFile.FullName)) if (File.Exists(_configFile.FullName))
{ {
var existingJson = File.ReadAllText(_configFile.FullName); var existingJson = ReadConfigFile(_configFile.FullName);
_configData = JsonConvert.DeserializeObject<Dictionary<string, object>>(existingJson) ?? _configData = JsonConvert.DeserializeObject<Dictionary<string, object>>(existingJson) ??
new Dictionary<string, object>(); new Dictionary<string, object>();
} }
@@ -39,6 +43,21 @@ public class Config
} }
} }
// 读取配置文件的辅助方法,使用自定义读取器或默认方法
private string ReadConfigFile(string filePath)
{
return _customReader != null ? _customReader(filePath) : File.ReadAllText(filePath);
}
// 写入配置文件的辅助方法,使用自定义写入器或默认方法
private void WriteConfigFile(string filePath, string content)
{
if (_customWriter != null)
_customWriter(filePath, content);
else
File.WriteAllText(filePath, content);
}
public T? Get<T>(string key) public T? Get<T>(string key)
{ {
return Get<T>(key, default); return Get<T>(key, default);
@@ -104,7 +123,7 @@ public class Config
currentData[finalKey] = value!; currentData[finalKey] = value!;
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented); var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json); WriteConfigFile(_configFile.FullName, json);
} }
public void Delete(string key) public void Delete(string key)
@@ -130,6 +149,6 @@ public class Config
currentData.Remove(finalKey); currentData.Remove(finalKey);
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented); var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json); WriteConfigFile(_configFile.FullName, json);
} }
} }

View File

@@ -13,6 +13,39 @@
var config = new Config(new FileInfo("./config.json")); var config = new Config(new FileInfo("./config.json"));
``` ```
```csharp
// 定义自定义读取方法
Config.ConfigReader customReader = (filePath) => {
Console.WriteLine($"正在读取文件: {filePath}");
if (File.Exists(filePath))
{
var content = File.ReadAllText(filePath);
Console.WriteLine($"文件内容: {content}");
return content;
}
return "{}";
};
// 定义自定义保存方法
Config.ConfigWriter customWriter = (filePath, content) => {
Console.WriteLine($"正在保存到文件: {filePath}");
// 创建备份
if (File.Exists(filePath))
{
var backupPath = filePath + ".backup";
File.Copy(filePath, backupPath, true);
Console.WriteLine($"已创建备份: {backupPath}");
}
File.WriteAllText(filePath, content);
Console.WriteLine("文件保存完成");
};
// 使用自定义方法创建 Config 实例
var config = new Config(new FileInfo("./config.json"), customReader, customWriter);
```
### 读取键值 ### 读取键值
> 若键不存在则返回默认值,并且同时也会写入键值(若没有提供默认值则写入空) > 若键不存在则返回默认值,并且同时也会写入键值(若没有提供默认值则写入空)

View File

@@ -1,4 +1,5 @@
using JackCraft.Config; using System.Text;
using JackCraft.Config;
namespace Test; namespace Test;
@@ -12,13 +13,104 @@ internal static class Program
Test = "test", Test = "test",
Abc = 123 Abc = 123
}; };
var config = new Config(new FileInfo("./config.json"));
var a = config.Get("aaa.www.www", "555");
var b = config.Get("aaa.bbb", "1");
var c = config.Get("aaa.sss", "1");
var d = config.Get("bbb.sss", "1");
config.Delete("aaa.www"); // 示例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<string>("app.name")}");
Console.WriteLine($"App Version: {config1.Get<string>("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<string>("app.name")}");
Console.WriteLine($"App Version: {config2.Get<string>("app.version")}");
Console.WriteLine($"App Author: {config2.Get<string>("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<string>("secret.apiKey")}");
Console.WriteLine($"Password: {config3.Get<string>("secret.password")}");
Console.WriteLine("\n所有示例执行完成");
} }
} }