locked
appendalltext to file RRS feed

  • Question

  • User-1623466995 posted

    Would there be a reason why appending some text to a txt file works when I put a page break and step through it manually(F11), but it doesn't work if I run it normally?

     

     while (count != 8)
                    {
    
    
                        File.AppendAllText(MyFile.ToString(), portsw.ToString());
                        count++;
                    }


     

    Tuesday, April 23, 2013 9:04 PM

Answers

  • User1814019480 posted

    Hi EducateMe,

    Thanks for your posting.

    I modified your code and tested it. The result was well.

    I think if you open the file always,you can not see the changes of TXT file.

    I think you need to close file before you put a page break and step through it manually(F11),So you can get result normally.

    You can open the txt file afer you put a page break and step .

    The blow code is my test.

    this code is ok:

    while (count != 8)
             {
                 File.AppendAllText(@"C:\Users\XXX\Desktop\temp.txt", "content");
                 count++;
             }
    

    And there is another way of writing text in txt file .you can refer to it.

    StreamWriter sw = new StreamWriter(@"C:\Users\xxx\Desktop\temp1.txt");
    
            sw.WriteLine("----------------hello----------------");
    
            sw.WriteLine("content");
    
            sw.WriteLine("----------------end----------------");
    
            sw.Flush();
    
            sw.Close();
    

    Hop this helps.

    Regards!

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2013 10:33 PM

All replies

  • User1508394307 posted

    Do you get any error at runtime? Maybe file is locked since you try to change it in a loop.

    The code is not very clear. Maybe it makes sense to do

    StringBuilder sb = new StringBuilder();
    
    while (count++ != 8) sb.Append(portsw.ToString());
    File.AppendAllText(MyFile.ToString(), sb.ToString());

    Hope this helps.

    Wednesday, April 24, 2013 3:11 AM
  • User1814019480 posted

    Hi EducateMe,

    Thanks for your posting.

    I modified your code and tested it. The result was well.

    I think if you open the file always,you can not see the changes of TXT file.

    I think you need to close file before you put a page break and step through it manually(F11),So you can get result normally.

    You can open the txt file afer you put a page break and step .

    The blow code is my test.

    this code is ok:

    while (count != 8)
             {
                 File.AppendAllText(@"C:\Users\XXX\Desktop\temp.txt", "content");
                 count++;
             }
    

    And there is another way of writing text in txt file .you can refer to it.

    StreamWriter sw = new StreamWriter(@"C:\Users\xxx\Desktop\temp1.txt");
    
            sw.WriteLine("----------------hello----------------");
    
            sw.WriteLine("content");
    
            sw.WriteLine("----------------end----------------");
    
            sw.Flush();
    
            sw.Close();
    

    Hop this helps.

    Regards!

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2013 10:33 PM