none
Why this not writing to txt file RRS feed

  • Question

  • Hi i get no error when debuggin but it doesent seem to insert the text into the txtfile

    here is my code


            private void writetotextfiles()
            {
               
                StringBuilder sb = new StringBuilder();
               sb.AppendLine(txtPath.Text);
               sb.AppendLine(txtSourceSql.Text);
               sb.AppendLine(txtUsernameSql.Text);
                sb.AppendLine(txtDBNameSql.Text);
                      

                   
                using (StreamWriter outfile =
                    new StreamWriter("TextFile1.txt"))
                {
                    String PathtoFile = "TextFile.txt";
                    File.WriteAllText(PathtoFile, sb.ToString());
                }
            }

    Tuesday, August 31, 2010 10:40 AM

Answers

  • hi,

     

    it works fine for me. but the question is why you use streamwriter and File? you initialize streamwriter for TextFile1.txt and then write data into TextFile.txt. either you use File.WriteAllText(PathtoFile, sb.ToString()); or

    using (StreamWriter outfile = new StreamWriter("TextFile1.txt"))
          {
            outfile.Write(sb.ToString());
          }


    BR, Karol. mark as answer/vote as helpful if it helped you
    • Marked as answer by Magic Mushroom Tuesday, August 31, 2010 11:26 AM
    Tuesday, August 31, 2010 10:46 AM

All replies

  • hi,

     

    it works fine for me. but the question is why you use streamwriter and File? you initialize streamwriter for TextFile1.txt and then write data into TextFile.txt. either you use File.WriteAllText(PathtoFile, sb.ToString()); or

    using (StreamWriter outfile = new StreamWriter("TextFile1.txt"))
          {
            outfile.Write(sb.ToString());
          }


    BR, Karol. mark as answer/vote as helpful if it helped you
    • Marked as answer by Magic Mushroom Tuesday, August 31, 2010 11:26 AM
    Tuesday, August 31, 2010 10:46 AM
  • If you want to append text to a file (creating the file if necessary) try this instead:

    List<string> lines = new List<string>();
    lines.Add("Hello");
    lines.Add("Goodbye");
    File.AppendAllLines("TextFile1.txt", lines);
    

    If you want to replace the file altogether, change the last line to File.WriteAllLines("TextFile1.txt", lines).

    HTH,
    ShaneB

    Tuesday, August 31, 2010 11:08 AM