config.json读取和存储
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读取和存储的更多相关文章
- [walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json
说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化. 首先,理论部分看[汤姆大叔的博客] 解读AS ...
- 微信小程序 project.config.json 配置
可以在项目根目录使用 project.config.json 文件对项目进行配置. miniprogramRoot Path String 指定小程序源码的目录(需为相对路径) qcloudRoot ...
- docker error:/root/.docker/config.json: is a directory
问题: 本地没有taskworker镜像,docker从远端拉取,但是拉取时需要读取config.json配置,解析配置时,发现config.json是个目录,错误信息如下: taskworker_1 ...
- Pandas_数据读取与存储数据(全面但不精炼)
Pandas 读取和存储数据 目录 读取 csv数据 读取 txt数据 存储 csv 和 txt 文件 读取和存储 json数据 读取和存储 excel数据 一道练习题 参考 Numpy基础(全) P ...
- Pandas_数据读取与存储数据(精炼)
# 一,读取 CSV 文件: # 文字解析函数: # pd.read_csv() 从文件中加载带分隔符的数据,默认分隔符为逗号 # pd.read_table() 从文件中加载带分隔符的数据,默认分隔 ...
- Python json 读取 json 文件并转为 dict
Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...
- springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法
springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...
- 微信小游戏 项目配置文件 project.config.json
一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.json,在工具上做的任何配置都会写入到这个文件,当重新安装工具或 ...
- 微信小程序:工具配置 project.config.json
微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...
随机推荐
- 《精通并发与Netty》学习笔记(11 - 详解NIO (二) 分散/聚集 Scatter/Gather、Selector)
一.分散/聚集 Scatter/Gather scatter/gather指的在多个缓冲区上实现一个简单的I/O操作,比如从通道中读取数据到多个缓冲区,或从多个缓冲区中写入数据到通道:scatter( ...
- vue中 :style 与 :class 三元运算符使用
参考链接:https://www.jianshu.com/p/31664974303d
- java中类加载的全过程及内存图分析
类加载机制: jvm把class文件加载到内存,并对数据进行校验.解析和初始化,最终形成jvm可以直接使用的java类型的过程. (1)加载 将class文件字节码内容加载到内存中,并将这些静态数据转 ...
- 44.python排序算法(冒泡+选择)
一,冒泡排序: 是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个,如果他们的排序错误就把他们交换过来. 冒泡排序是稳定的(所谓稳定性就是两个相同的元素不会交换位置) 冒泡排序算法的运作如下 ...
- Oracle定时调用存储过程
#1Demo: 1.创建表 create table job_table(run_time date); 2.创建存储过程 create or replace procedure job_proc i ...
- java中Super到底是什么意思?必须举例说明!
[学习笔记] 3."超"关键字(super keyword) Super是一个参考(或说指针)指向他紧邻的父类(见下面的例子).用super可以指向被隐藏的父类的同名成员. 3.1 ...
- 后台返回数据为map集合,前端js处理方法
当后台返回的数据不是json而是map集合的时候,前端js中处理就将其看作是一个数组,例如后台返回的代码入下: Map<String, String> result = new HashM ...
- spring mvc 简单实现及相关配置实现
配置文件 actio.xml <?xml version="1.0" encoding="UTF-8"?> <controller> & ...
- Mancala II
题目描述 Mancala is a family of board games played around the world, sometimes called sowing games, or c ...
- 通过ADB调试安卓程序
ADB,即 Android Debug Bridge,它是Android开发/测试人员不可替代的强大工具. 1.下载ADB后,将以下四个文件放到某个文件夹下即可.因为打开Cmd默认路径是 C:\Use ...