skip to Main Content

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


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

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

    instead.

    Login or Signup to reply.
  2. 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);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search