I'm saving a text file at application startup and reading this text file from application startup.
This is not saving my file at application startup, what's wrong with this code?
Saving text file at application startup code.
private void Savebutton1_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(Application.StartupPath + "Book.txt", true);
string json = JsonConvert.SerializeObject(vals);
sw.Write(json);
MessageBox.Show("Book Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
loading text file from application startup code.
string path = Path.Combine(Application.StartupPath, "ChequeBook.txt");
string textholder;
try
{
// Use StreamReader to consume the entire text file.
using (StreamReader reader = new StreamReader(path))
{
MessageBox.Show("Reached Here");
textholder = reader.ReadToEnd();
MessageBox.Show("Reached Here - 2");
}
if (textholder == string.Empty) {
return;
}
// Deserialise it from Disk back to a Dictionary
string jsonToRead = File.ReadAllText(textholder);
List<KeyValuePair<int, string>> myDictionaryReconstructed =
JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);