I have a file containing text and I can get it to populate a textbox on page load but it always adds a blank first line. Any ideas? I’ve tried skipping the first line in the array in case it was blank (both 0 and 1) but 0 does nothing and 1 skips the first line in the text file.
I’ve also tried to set the textbox to null
and ""
first in case it was appending to the textbox in some way.
//Populating the contents box
string[] str = null;
if (File.Exists(docPath + prefix + libIDPath + "\" + oldFileName))
{
str = File.ReadAllLines(docPath + prefix + libIDPath + "\" + oldFileName);
//str = str.Skip(0).ToArray();
//FDContentsBox.Text = null;
}
foreach (string s in str)
{
FDContentsBox.Text = FDContentsBox.Text + "n" + s;
}
2
Answers
In your
foreach
you are appending the "n" before appending the string itself. TryFDContentsBox.Text = FDContentsBox.Text + s + "n";
instead.
Please try this, there is no need to read all lines nor a foreach loop