【发布时间】:2021-03-26 20:23:26
【问题描述】:
我目前正在设置我的应用程序,我需要在我的 Windows 窗体中管理一个 json 文件(包含一些设置)。打开后,您可以轻松选择不同的设置,完成后,您可以保存它(这意味着我需要覆盖现有的一个 json 文件设置,并用新的设置替换它!
我尝试按照this 指南正确创建我的 json 文件!但我遇到了两个问题:
- 上面提到了这个解决方案,创建方括号(我不需要!)
- 似乎在一行上创建所有设置。这是正确的还是将来可能会出现问题?
一些材料......
我的申请:
原始json文件:
使用我的代码生成的 Json 文件:
我的班级:
public class data
{
public bool avx { get; set; }
public bool memory_pool { get; set; }
public bool smt { get; set; }
public bool spectre { get; set; }
public bool unlock_menu { get; set; }
public bool vinput { get; set; }
public double cpu_memory_pool_fraction { get; set; }
public double gpu_memory_pool_fraction { get; set; }
}
我的代码:
private void btn_save_Click(object sender, EventArgs e)
{
string settings_path = general_path + "\\plugins\\cyber_engine_tweaks\\" + "config.json"; //path
bool avx_set = cb_avx.Checked;
bool smt_set = cb_smt.Checked;
bool memory_set = cb_memory.Checked;
bool spectre_set = cb_spectre.Checked;
bool debug_set = cb_debug.Checked;
bool vinput_set = cb_vinput.Checked;
List<data> _data = new List<data>();
_data.Add(new data()
{
avx = avx_set,
cpu_memory_pool_fraction = 0.5,
gpu_memory_pool_fraction = 1.0,
memory_pool = memory_set,
smt = smt_set,
spectre = spectre_set,
unlock_menu = debug_set,
vinput = vinput_set
});
using (StreamWriter file = File.CreateText(settings_path))
{
JsonSerializer serializer = new JsonSerializer();
//serialize object directly into file stream
serializer.Serialize(file, _data);
}
}
【问题讨论】:
标签: c# json class settings jsonserializer