-1

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);
dandan78
  • 13,328
  • 13
  • 64
  • 78
Patrick
  • 217
  • 1
  • 5
  • 19
  • What is the problem that this single line of code gives you? Perhaps you should add the parts of code around this line to better understand your problem – Steve Aug 06 '16 at 12:27
  • You can read the application path http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application and combine it with your file –  Aug 06 '16 at 12:27
  • It sounds like you are running a console application which exits once it has completed all work. Stop it from exiting by having it ask for input with something like `Console.ReadKey();` – Crowcoder Aug 06 '16 at 12:29
  • @Steve this single line of code reads the file from what I defined in the path and not from my application folder where. I want to read all texts at once and store in jsonToRead, I want to use this method only, but this doensn't seem to have read a text file from application path, what should i call it method.. – Patrick Aug 06 '16 at 12:29
  • This line tries to read a file from whatever is the current directory at the moment of the call. If you need to read this file in a different position then you need to add the path to the file name. If, by Application Path you mean the location where your executable is stored then you can use the link provided by @x... above – Steve Aug 06 '16 at 12:36
  • Also [this link could help to understand](http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) the various options available under which is hidden the meaning of Application Path – Steve Aug 06 '16 at 12:41

1 Answers1

1

I wanted to save & write text file in application folder using File.CreateText method.

Found the First Answer:

This will write and save the text file in application folder.

        using (StreamWriter sw = File.CreateText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Book.txt")))
        {

            string json = JsonConvert.SerializeObject(vals);
            sw.Write(json);
        }
        MessageBox.Show("Book Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

I wanted to read the text file from application folder.

This will read the contents of the text file from application folder.

string jsonToRead = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Book.txt"));
Patrick
  • 217
  • 1
  • 5
  • 19