c# streamwriter generated .txt file is not completed

Spellman.Lau 101 Reputation points
2021-08-31T01:24:26.067+00:00

Hi,

I have two string lists and use a c# winform app to put the list elements into a .txt file.
Those two lists have the same length. For instance, each list has 436 elements.
I can see all elements are generated in the VS2019.
127745-list2.png
However, in the generated .txt file, the last few elements are not there. This screenshot indicates the file is cut at M429. Sometimes, it is cut at M434. It varies randomly.
127697-txt.png

Is there a limitation somewhere? Could you please help on this issue? Thanks.

Here is the code:

                List<string> listTag = new List<string>();//List of Tag  
                List<string> listDes = new List<string>();//List of Description  
                    string strTxtName = strPrjPath + "\\Alm_Cfg.txt";  
                    if (File.Exists(strTxtName)) File.Delete(strTxtName);  
                    StreamWriter sw = File.CreateText(strTxtName);  
                    sw.WriteLine("<triggers>");  
                    for (int i = 1; i <= listTag.Count; i++)  
                    {  
                        sw.WriteLine("<trigger id=\"T" + i.ToString() + "\" type=\"value\" ack-all-value=\"0\" use-ack-all=\"false\" ack-tag=\"\" exp=\"{[" + tbxDevSC.Text + "]" + listTag[i - 1] + "}\" message-tag=\"\" message-handshake-exp=\"\" message-notification-tag=\"\" remote-ack-exp=\"\" remote-ack-handshake-tag=\"\" label=\"" + listTag[i - 1] + "\" handshake-tag=\"\"/>");  
                    }  
                    sw.WriteLine("</triggers>");  
                    sw.WriteLine("<messages>");  
                    for (int i = 1; i <= listTag.Count; i++)  
                    {  
                        sw.WriteLine("<message id=\"M" + i.ToString() + "\" trigger-value=\"1\" identifier=\"" + i + "\" trigger=\"#T" + i + "\" backcolor=\"#800000\" forecolor=\"#FFFFFF\" audio=\"false\" display=\"true\" print=\"false\" message-to-tag=\"false\" text=\"" + listDes[i - 1] + "\"/>");  
                    }  
                    sw.WriteLine("</messages>");  
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2021-08-31T03:00:20.057+00:00

    Do you Close the StreamWriter object?

    sw.Close();

    StreamWriter.Close Method

    https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.close?view=net-5.0#System_IO_StreamWriter_Close

    "You must call Close to ensure that all data is correctly
    written out to the underlying stream."

    • Wayne
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-08-31T07:07:34.727+00:00

    You can also wrap the StreamWriter in a using statement, which provides a convenient syntax that ensures the correct use of IDisposable objects.

                using (StreamWriter sw = File.CreateText(strTxtName))  
                {  
                    ......  
                }     
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.