Hi
Thanks for your help so far but I am not sure exactly what the solution is? Are you sugesting the while loop code should be omitted? I did this below:
public string PblReadFile()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
//while (sr.Peek() > -1)
//{
//lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
//}
return sr.ReadLine();
sr.Close();
fsIn.Close();
} This compiles fine but does not do anything. Can you provide a bit more clarification please.
No, the while loop is an essential part to make you possible to read all the lines of the text files using StreamReader.ReadLine().
Before I continue, can you explain first, what should method PblReadFile return? In your program, it return one/single string, but actually the data on your text file is multi-line, so what do you want? do you want to return the whole text file inside one string? or actually you want a multi string (one string for each lines)?
My best guess is you better return an array of string, so it'is also more easier for you to loop the array, and put each string item into a listbox.
if not, use StreamReader.ReadToEnd(), return it as single string, then split it using String.Split("\n\r"), and put it into listbox one by one.
Regards,
Lok