public static class LocalSetupHelper { #region 欄位 /// <summary> /// json文本 /// </summary> private static string json; /// <summary> /// 指定保存路徑 /// </s ...
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)) { dynamic jsonObj = JsonConvert.DeserializeObject(json); 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 }View Code
調用方法:
LocalSetupHelper.SetData(Sss.維護, "密碼", "123456"); //保存
var c=LocalSetupHelper.GetData(Sss.前臺, "密碼"); 獲取
LocalSetupHelper.RemovePropertyName(Sss.前臺, "密碼"); //清除子節點
LocalSetupHelper.RemovePropertyName(Sss.前臺); 清除父節點
enum Sss
{
維護,
前臺,
}
輸出格式:
{
"前臺": {
"密碼": "123456",
},
"維護": {
"密碼": "123456"
}
}
當然也有這種節點的
{
"密碼": "123456",
"密碼xxx": "123456"
}
可以參考前面的連接 :(Newtonsoft)Json增刪改查(這是單個對象) - 22222220 - 博客園 (cnblogs.com)
當然這個沒仔細看 可能有錯誤 也沒測 自己改改 或者評論區留言 我改