我能够从文件中成功读取值,但是我不知道如何写特定的名称。
我开始尝试的是
public void writeCharacter()
{
Form1 f1 = new Form1();
string homepath = Environment.GetEnvironmentVariable("homepath");
try
{
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer jserialize = new JsonSerializer();
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jserialize.Serialize(jw, ctrl.Text);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Source + "\n\n" + e.Message);
}
}
唯一的问题是我无法弄清楚序列化器要写入的位置,也无法弄清楚如何指定要写入的名称。
我想从文本框中获取输入(更改文本后,它们会被赋予标签“ CHANGED”),并且根据数据来自哪个文本框,我希望将其写入该特定名称。任何帮助深表感谢!!
我的代码全部托管在git上
您已经有一个JsonWriter
,而无需分配一个JsonSerializer
。AJsonWriter
是用于写出JSON的较低级机制:
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json",
FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jw.WritePropertyName(ctrl.Name);
jw.WriteValue(ctrl.Text);
}
}
}
该JsonWriter
会通过您写StreamWriter
,该文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句