75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using MAX.Core.DependencyInjection;
|
|
using MAX.Framework.Configs;
|
|
using MAX.Framework.Interfaces;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace MAX.Framework.Helpers
|
|
{
|
|
/// <summary>
|
|
/// 配置
|
|
/// </summary>
|
|
public class ConfigHelper
|
|
{
|
|
private static IFolderConfig _folderConfig = LazyServiceProvider.Instance.LazyGetService<IFolderConfig>();
|
|
public static string prvategp = "4Z-zy_VP";//pIuEFX-0-OZm%tP2qtKw
|
|
public static T LoadConfig<T>() where T : ConfigBase
|
|
{
|
|
string filename = Activator.CreateInstance<T>().ConfigName;
|
|
return LoadConfig<T>(filename);
|
|
}
|
|
|
|
public static T LoadConfig<T>(string fileName) where T : ConfigBase
|
|
{
|
|
try
|
|
{
|
|
string filePath = GetConfigUserPath(fileName);
|
|
if (File.Exists(filePath))
|
|
{
|
|
string content = File.ReadAllText(filePath, Encoding.UTF8);
|
|
return JsonConvert.DeserializeObject<T>(content);
|
|
}
|
|
}
|
|
catch { }
|
|
return default;
|
|
}
|
|
|
|
public static void SaveConfig<T>(T config) where T : ConfigBase
|
|
{
|
|
string filename = Activator.CreateInstance<T>().ConfigName;
|
|
SaveConfig(filename, config);
|
|
}
|
|
|
|
public static void SaveConfig<T>(string fileName, T config) where T : ConfigBase
|
|
{
|
|
try
|
|
{
|
|
string filePath = GetConfigUserPath(fileName);
|
|
FileHelper.CreateDirectory(filePath);
|
|
string content = JsonConvert.SerializeObject(config);
|
|
File.WriteAllText(filePath, content, Encoding.UTF8);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public static void SaveConfig(string fileName, string content)
|
|
{
|
|
try
|
|
{
|
|
string filePath = GetConfigUserPath(fileName);
|
|
FileHelper.CreateDirectory(filePath);
|
|
File.WriteAllText(filePath, content, Encoding.UTF8);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public static string GetConfigUserPath(string fileName)
|
|
{
|
|
string filePath = Path.Combine(_folderConfig.LocalAppFolder, "config", fileName);
|
|
return filePath;
|
|
}
|
|
}
|
|
}
|