Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading