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;
}
}
}

  

config.json读取和存储的更多相关文章

  1. [walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json

    说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化. 首先,理论部分看[汤姆大叔的博客] 解读AS ...

  2. 微信小程序 project.config.json 配置

    可以在项目根目录使用 project.config.json 文件对项目进行配置. miniprogramRoot Path String 指定小程序源码的目录(需为相对路径) qcloudRoot ...

  3. docker error:/root/.docker/config.json: is a directory

    问题: 本地没有taskworker镜像,docker从远端拉取,但是拉取时需要读取config.json配置,解析配置时,发现config.json是个目录,错误信息如下: taskworker_1 ...

  4. Pandas_数据读取与存储数据(全面但不精炼)

    Pandas 读取和存储数据 目录 读取 csv数据 读取 txt数据 存储 csv 和 txt 文件 读取和存储 json数据 读取和存储 excel数据 一道练习题 参考 Numpy基础(全) P ...

  5. Pandas_数据读取与存储数据(精炼)

    # 一,读取 CSV 文件: # 文字解析函数: # pd.read_csv() 从文件中加载带分隔符的数据,默认分隔符为逗号 # pd.read_table() 从文件中加载带分隔符的数据,默认分隔 ...

  6. Python json 读取 json 文件并转为 dict

    Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...

  7. springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法

    springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...

  8. 微信小游戏 项目配置文件 project.config.json

    一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.json,在工具上做的任何配置都会写入到这个文件,当重新安装工具或 ...

  9. 微信小程序:工具配置 project.config.json

    微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...

随机推荐

  1. [BAT] cmd 管理员权限 右键菜单 运行

    将下面保存为 *.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell\runa ...

  2. .Net WebApi接口之Swagger集成详解

    本文详细的介绍了.net从一个新的项目中创建api后集成swagger调试接口的流程! 1.首先我们创建一个MVC项目(VS2012): 2.然后在项目中的Controllers文件夹中添加API接口 ...

  3. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

  4. CodeBlocks 配置

    CodeBlocks 配置 Code::Blocks 17.12 时间:2019.6 下载网址 http://www.codeblocks.org/downloads/26 ,这里选择的是 mingw ...

  5. POJ3046ANT_COUNTING

    #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...

  6. python-day3(正式学习)

    执行python的两种方式 交互式 优点:运行一句执行一句,方便修改 缺点:关闭即消失,无法保存 命令行式 优点:能一直保存 缺点:代码全部写完才可以调试bug 以后尽量使用pycharm和jupyt ...

  7. 二进制协议gob和msgpack介绍

    二进制协议gob和msgpack介绍 本文主要介绍二进制协议gob及msgpack的基本使用. 最近在写一个gin框架的session服务时遇到了一个问题,Go语言中的json包在序列化空接口存放的数 ...

  8. AlertManager 部署及使用

    熟悉了 Grafana 的报警功能,但是 Grafana 的报警功能目前还比较弱,只支持 Graph 的图表的报警.今天来给大家介绍一个功能更加强大的报警工具:AlertManager. 简介 之前我 ...

  9. 4-MySQL DBA笔记-开发进阶

    第4章 开发进阶 本章将介绍一些重中之重的数据库开发知识.在数据库表设计中,范式设计是非常重要的基础理论,因此本章把它放在最前面进行讲解,而这其中又会涉及另一个重要的概念——反范式设计.接下来会讲述M ...

  10. Spring 自定义Bean 实例获取

    一.通过指定配置文件获取, 对于Web程序而言,我们启动spring容器是通过在web.xml文件中配置,这样相当于加载了两次spring容器 ApplicationContext ac = new ...