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 ...
随机推荐
- TensorFlow.训练_资料(有视频)
ZC:自己训练 的文章 貌似 能度娘出来很多,得 自己弄过才知道哪些个是坑 哪些个好用...(在CSDN文章的右侧 也有列出很多相关的文章链接)(貌似 度娘的关键字是"TensorFlow ...
- beego项目和go项目 打包部署到linux
参考文章: https://www.jianshu.com/p/64363dff9721 [beego项目] 一. 打包 1. 打开Terminal 定位到工程的 main.go 文件夹目录 2. 执 ...
- spring中的BeanFactoryPostProcessor
spring中的BeanFactoryPostProcessor和BeanPostProcessor有些像,BeanPostProcessor是在bean的初始化前后进行一些操作, BeanFacto ...
- 47.前端css学习、登陆页面的制作
CSS: 有了CSS,html中大部分表现样式的标签就可以不用了 html只负责文档的结构和内容,表现形式完全交给CSS,html文档变得更加简洁 CSS的引入方式: 内联式引入:直接赋予标签styl ...
- Python Requests库 form-data 上传文件操作
请求数据示例: ------WebKitFormBoundaryKLoWgrA4O40MayHM Content-Disposition: form-data; name="id" ...
- 熟练使用Github
---恢复内容开始--- 1.开头 GIT地址 https://github.com/Untrara GIT用户名 Untrara 学号后五位 31124 博客地址 https://i.cnblog ...
- windows 清理 cbs.log 文件
请参考以下步骤 1.win+r输入 services.msc进入服务,找到并双击Windows Modules Installer点击停止, 2.然后就可以手动删除 3.按一的步骤开启TrustedI ...
- IT学习的计算机网络内容
1.一种结构:数据结构 参考书目:<大话数据结构>.<数据结构(C#语言描述)>.<剑指Offer> ①线性表部分: 线性表(上){ 数组.ArrayList } ...
- 聊聊BIO、NIO与AIO的区别
题目:说一下BIO/AIO/NIO 有什么区别?及异步模式的用途和意义? 1F 说一说I/O首先来说一下什么是I/O? 在计算机系统中I/O就是输入(Input)和输出(Output)的意思,针对不同 ...
- Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)
Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...