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. mysql5.6.36 编译报错make[1]: *** [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all]..

    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_UNIX_ADDR=/tmp/m ...

  2. 十步学习法 -- 来自<<软技能>>一书的学习方法论

    <<软技能>>第三篇“学习”,作者讲述了自己的学习方法:十步学习法.下面我用编程语言的方式来介绍. 十步学习法 伪代码介绍 # **这一步的目的不是要掌握整个主题,而是对相关内 ...

  3. manjaro Docker环境配置记

    1.系统配置如下: ‘by: /home/inkhin/桌面/深度截图_选择区域_20191004145104.png [吐槽: ChromeLinux版居然不能用博客园TinyMce的上传图片功能] ...

  4. DS博客作业--07查找

    目录 DS博客作业--07查找 1.本周学习总结(0--2分) 1.思维导图 2.谈谈你对查找运算的认识及学习体会. 2.PTA实验作业(6分) 2.1.题目1:6-1 二叉搜索树的操作集 (30 分 ...

  5. NLP文本清理时常用的python小函数

    # coding = utf-8 import re 1. 清理杂七杂八字符 ''' [a-zA-Z0-9] 字母数字 [\u4e00-\u9fa5] 汉字的utf-8 code范围 ''' # 保留 ...

  6. [Agc030B]Tree Burning_贪心

    Tree Burning 题目链接:https://atcoder.jp/contests/agc030/tasks/agc030_b 数据范围:略. 题解: 开始以为是左右左右这样,发现过不去样例. ...

  7. Linux系列(17)之系统服务

    我们知道,在我们登陆Linux后,系统就为我们提供了很多服务,比如例行工作调度服务crond.打印服务.邮件服务等.那么这些服务是如何被启动的呢? 这个问题先放一下,接下来我们先了解一下Linux的启 ...

  8. Windows32或64位下载安装配置Spark

    [学习笔记] Windows 32或64位下载安装配置Spark:1)下载地址:http://spark.apache.org/downloads.html 马克-to-win @ 马克java社区: ...

  9. Windows应急响应常见命令

    ---恢复内容开始--- 1.查看所有连接的PID netstat -ano 2.过滤特定端口 netstat -ano | findstr “443” 3.查看占用443端口的进程 tasklist ...

  10. logstash grok

    input { file { path => "/opt/service/test-service/logs/catalina-error*.log" type => ...