Home > Net >  Populating a Textbox with a Text File but it always adds a blank first line?
Populating a Textbox with a Text File but it always adds a blank first line?

Time:10-08

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;
}

CodePudding user response:

In your foreach you are appending the "\n" before appending the string itself. Try

FDContentsBox.Text = FDContentsBox.Text s "\n";

instead.

CodePudding user response:

Please try this, there is no need to read all lines nor a foreach loop

var filePath = docPath   prefix   libIDPath   "\\"   oldFileName;
if (File.Exists(filePath))
{
    FDContentsBox.Text = File.ReadAllText(filePath);
}
  • Related