question

SpellmanLau-1757 avatar image
0 Votes"
SpellmanLau-1757 asked SpellmanLau-1757 edited

c# streamwriter generated .txt file is not completed

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>");


dotnet-csharp
list2.png (14.0 KiB)
txt.png (4.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered SpellmanLau-1757 commented

Do you Close the StreamWriter object?

sw.Close();

StreamWriter.Close Method

https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. It does resolve the issue.

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered SpellmanLau-1757 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. This also fixed the issue.

0 Votes 0 ·