Hello,
ever since I put "using (var stream = File.Create(path_combined)){}" code line, my deserialization stoped working.
When I re-run the program the data is not fetched from json txt (in txt it updates successfully)
I don't use streamline to write or read, do you have any suggestions?
[Serializable]
public partial class Form1 : Form
{
//bool var = Class1.flag;
//FILENAME
string input = Interaction.InputBox("Enter a serial number", "TEST", "Default", -1, -1);
//DEFAULT PATH ROOT
String root = @".\\";
Class1 c1 = new Class1();//holds method's attribute flags
Class1 c2 = new Class1(); //holds method flags whether the used or not
string path_combined;
public Form1()
{
InitializeComponent();
input += ".txt";
//default path + new filename
path_combined = Path.Combine(root, input);
if (!File.Exists(path_combined))
{
//File.Create(path_combined);
using (var stream = File.Create(path_combined))
{
if (new FileInfo(path_combined).Length > 0)
{
//flag situation
string json2 = File.ReadAllText(path_combined);
FormatJson(json2);
c1 = JsonConvert.DeserializeObject<Class1>(json2);
//Method used or not
string json = File.ReadAllText(path_combined);
FormatJson(json);
c2 = JsonConvert.DeserializeObject<Class1>(json);
}
}
}
}
private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
// SET TRUE
private void Button1_Click(object sender, EventArgs e)
{
c1.flag = true;
c1.flag2 = true;
c2.M1 = true;
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
File.WriteAllText(path_combined, json);
string json2 = JsonConvert.SerializeObject(c2, Formatting.Indented);
File.WriteAllText(path_combined, json2);
}