C# Newtonsoft增删改查(本地存储)(简单便捷)(拿来即用)
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 = JToken.FromObject(propertyValue);
json = jsonObj.ToString();
}
private static object GetValue(string json, string propertyName)
{
dynamic jsonObj = JsonConvert.DeserializeObject(json);
return jsonObj;
}
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.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 == null)
{
jsonObj.Add(id.ToString(), new JObject());
}
jsonObj = 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;
}
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");//保存<br>
var c=LocalSetupHelper.GetData(Sss.前台, "密码");获取<br><br>LocalSetupHelper.RemovePropertyName(Sss.前台, "密码"); //清除子节点<br><br>LocalSetupHelper.RemovePropertyName(Sss.前台); 清除父节点<br><br><br> enum Sss
{
维护,
前台,
}
输出格式:
{
"前台": {
"密码": "123456",
},
"维护": {
"密码": "123456"
}
}
当然也有这种节点的
{
"密码": "123456",
"密码xxx": "123456"
}
可以参考前面的连接 :(Newtonsoft)Json增删改查(这是单个对象) - 22222220 - 博客园 (cnblogs.com)
当然这个没仔细看 可能有错误 也没测 自己改改 或者评论区留言 我改
来源:https://www.cnblogs.com/jiaozai891/archive/2023/02/16/17127708.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]