json格式的配置文件的读取和存储

    public class ConfigHelper
{
public static T GetConfig<T>(string path)
{
if (string.IsNullOrEmpty(path))
return default(T); try
{
string strConfig = FileHelper.ReadFromFile(path);
if (string.IsNullOrEmpty(strConfig))
return default(T); return JsonConvert.DeserializeObject<T>(strConfig); }
catch (Exception)
{
return default(T);
}
} public static bool SaveConfig<T>(string path, T t)
{
try
{
if (string.IsNullOrEmpty(path) || t == null)
return false; string strConfig = JsonConvert.SerializeObject(t);
if (string.IsNullOrEmpty(strConfig))
return false; if (!FileHelper.WriteToFile(path, strConfig))
return false; return true;
}
catch (Exception)
{
return false;
}
}
}

  

    public class FileHelper
{
public static string ReadFromFile(string path)
{
if (string.IsNullOrEmpty(path) || !File.Exists(path))
return null; try
{
using (StreamReader reader = new StreamReader(path, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
} public static bool WriteToFile(string path, string content)
{
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(content))
return false; try
{
using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
{
writer.WriteLine(content);
}
return true;
}
catch (Exception)
{
return false;
}
}
}

  

最新文章

  1. MongoDB的TruncationException异常解决方法
  2. jq最新前三篇文章高亮显示
  3. 【Web】Eclipse + Maven + Struts搭建服务器
  4. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
  5. Equipment Box[HDU1110]
  6. ArrayList集合排序
  7. Arduino 3G shield using SoftwareSerial to control
  8. 腾讯qq等级计算公式面试题
  9. markdowm写博客测试
  10. Docker的使用初探(一):常用指令说明
  11. 2018-2019-3 网络对抗技术 20165305 Exp3 免杀原理与实践
  12. Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)
  13. CRM的组织架构
  14. pxe+kickstart 自动化部署linux操作系统
  15. R语言:多个因变量时,如何在plot函数中画多条曲线(plot,points,lines,legend函数)
  16. Django后端项目----restful framework 认证源码流程
  17. IIS下载,WebClient().DownloadFile下载
  18. jdk源码-&gt;并发-&gt;Unsafe&amp;Atomic
  19. JVM(上)
  20. [php]几个常用函数

热门文章

  1. VSCode 怎么运行代码
  2. jackson 实体转json 为NULL或者为空不参加序列化【转载】
  3. VSCode插件Prettier配置
  4. 41.进程池--Pool
  5. [LuoguP1074]靶形数独_搜索
  6. error: audit:backlog limit exceeded
  7. 剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
  8. 3.解决git不可用问题
  9. Devexpress xaf用代码打开菜单(Navigation Item)
  10. 梯度直方图(HOG,Histogram of Gradient)