/// <summary>
/// 配置文件工具类
/// </summary>
public static class ConfigTools
{
public static string ReadText(string filePath)
{
try
{
string content = File.ReadAllText(filePath, Encoding.UTF8);
if (content == null)
content = string.Empty;
return content;
}
catch (Exception)
{
return string.Empty;
}
}
public static void SaveText(string filePath, string content)
{
try
{
string directory = filePath.Substring(0, filePath.LastIndexOf("\\"));
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
if (content == null)
content = string.Empty;
File.WriteAllText(filePath, content, Encoding.UTF8);
}
catch (Exception)
{
}
}
public static void SaveJson(object json, string filePath)
{
try
{
string directory = filePath.Substring(0, filePath.LastIndexOf("\\"));
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
string content = JsonTools.SerializeToString(json);
if (content == null)
content = string.Empty;
File.WriteAllText(filePath, content, Encoding.UTF8);
}
catch (Exception)
{
}
}
public static T ReadJson<T>(string filePath) where T : class
{
try
{
if (!File.Exists(filePath))
return null;
string content = File.ReadAllText(filePath, Encoding.UTF8);
return JsonTools.DeserializeToTFromString<T>(content);
}
catch (Exception)
{
return null;
}
}
}
配置文件工具类【ConfigTools】
最新推荐文章于 2024-08-23 10:49:56 发布