优化json读取类
This commit is contained in:
@@ -13,6 +13,20 @@ namespace AntdUIDemo.Utils
|
||||
{
|
||||
private static readonly string configFilePath = "appsettings.json";
|
||||
|
||||
public static JObject GetAppSettings(string configFilePath = "appsettings.json")
|
||||
{
|
||||
if (!File.Exists(configFilePath))
|
||||
{
|
||||
// 如果文件不存在,创建默认的配置文件
|
||||
CreateDefaultConfigFile(configFilePath);
|
||||
}
|
||||
|
||||
var json = File.ReadAllText(configFilePath);
|
||||
var jsonObj = JObject.Parse(json);
|
||||
|
||||
return (JObject)jsonObj["AppSettings"];
|
||||
}
|
||||
|
||||
public static void UpdateAppSetting(string key, string value)
|
||||
{
|
||||
// 检查配置文件是否存在
|
||||
|
||||
@@ -10,20 +10,6 @@ namespace AntdUIDemo.Utils
|
||||
{
|
||||
public class ConfigurationHelper
|
||||
{
|
||||
public static JObject GetAppSettings(string configFilePath = "appsettings.json")
|
||||
{
|
||||
if (!File.Exists(configFilePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Read the JSON file
|
||||
var json = File.ReadAllText(configFilePath);
|
||||
|
||||
// Parse the JSON object
|
||||
var jsonObj = JObject.Parse(json);
|
||||
|
||||
// Return the "AppSettings" section
|
||||
return (JObject)jsonObj["AppSettings"];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user