C# Newtonsoft增删改查(本地存储)(简单便捷)(拿来即用)
调用方法:
LocalSetupHelper.SetData(Sss.维护, "密码", "123456"); //保存
var c=LocalSetupHelper.GetData(Sss.前台, "密码"); 获取
LocalSetupHelper.RemovePropertyName(Sss.前台, "密码"); //清除子节点
LocalSetupHelper.RemovePropertyName(Sss.前台); 清除父节点
enum Sss
{
维护,
前台,
}
输出格式:
{
"前台": {
"密码": "123456",
},
"维护": {
"密码": "123456"
}
}
Demo: 拿来直接用
public static class LocalSetupHelper { #region 字段 /// <summary> /// json文本 /// </summary> private static string json; /// <summary> /// 指定保存路径 /// </summary> public static string path { get; set; } #endregion #region 构造函数 static LocalSetupHelper() { if (string.IsNullOrEmpty(path)) { string currentDirectory = Directory.GetCurrentDirectory(); path = currentDirectory + "\\LocalSetup.Data"; } if (!File.Exists(path)) { FileStream fs = File.Create(path); fs.Close(); } json = LoadFromFile(path); } #endregion #region 获取 /// <summary> /// 获取数据 /// </summary> /// <param name="id">父节点</param> /// <param name="propertyName">子节点</param> /// <returns></returns> public static object GetData(Enum id, string propertyName) { return GetDatas(id, propertyName); } #endregion #region 保存 /// <summary> /// 保存数据 /// </summary> /// <param name="id">父节点</param> /// <param name="propertyName">子节点名称</param> /// <param name="propertyValue">子节点值</param> public static void SetData(Enum id, string propertyName, object propertyValue) { SetData(ref json, id, propertyName, propertyValue); } #endregion #region 移除 /// <summary> /// 移除 /// </summary> /// <param name="id">父节点</param> /// <param name="propertyName">子节点</param> public static void RemovePropertyName(Enum id, string propertyName = null) { Remove(ref json, id, propertyName); } /// <summary> /// 清空数据 /// </summary> public static void ClearData() { json = string.Empty; SaveToFile(path); } #endregion #region 内部方法 private static void Add(ref string json, string propertyName, object propertyValue) { JObject jsonObj = JObject.Parse(json); jsonObj.Add(propertyName, JToken.FromObject(propertyValue)); json = jsonObj.ToString(); } private static void Update(ref string json, string propertyName, object propertyValue) { JObject jsonObj = JObject.Parse(json); jsonObj[propertyName] = JToken.FromObject(propertyValue); json = jsonObj.ToString(); } private static object GetValue(string json, string propertyName) { dynamic jsonObj = JsonConvert.DeserializeObject(json); return jsonObj[propertyName]; } private static void Remove(ref string json, Enum id, string propertyName = null) { dynamic jsonObj = JsonConvert.DeserializeObject(json); if (propertyName == null) { jsonObj.Remove(propertyName); } else { //jsonObj["总台"].Value<JObject>().Remove("账号"); jsonObj[id.ToString()].Remove(propertyName);//13.0版本 } json = JsonConvert.SerializeObject(jsonObj); SaveToFile(path); } private static void SetData(ref string json, Enum id, string propertyName, object propertyValue) { JObject jsonObj; if (!string.IsNullOrEmpty(json)) { jsonObj = JObject.Parse(json); } else { jsonObj = new JObject(); } if (jsonObj[id.ToString()] == null) { jsonObj.Add(id.ToString(), new JObject()); } jsonObj[id.ToString()][propertyName] = JToken.FromObject(propertyValue); json = jsonObj.ToString(); SaveToFile(path); } private static object GetDatas(Enum id, string propertyName) { if (!string.IsNullOrEmpty(json)) { JObject jsonObj = (JObject)JsonConvert.DeserializeObject(json); JObject frontEndObj = jsonObj[id.ToString()] as JObject; if (frontEndObj!=null&&frontEndObj.ContainsKey(propertyName)) { return jsonObj[id.ToString()][propertyName]; } } return null; } private static void SaveToFile(string filePath) { using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { using (var streamWriter = new StreamWriter(fileStream)) { streamWriter.Write(json); } } } private static string LoadFromFile(string filePath) { string[] lines = File.ReadAllLines(filePath); return string.Join("", lines); } #endregion }
当然也有这种节点的
{
"密码": "123456",
"密码xxx": "123456"
}
可以参考前面的连接 :(Newtonsoft)Json增删改查(这是单个对象) - 22222220 - 博客园 (cnblogs.com)