Do you Close the StreamWriter object?
sw.Close();
StreamWriter.Close Method
"You must call Close to ensure that all data is correctly
written out to the underlying stream."
- Wayne
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
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>");
Do you Close the StreamWriter object?
sw.Close();
StreamWriter.Close Method
"You must call Close to ensure that all data is correctly
written out to the underlying stream."
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.