By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

StreamReader read last line in a text file

P: n/a
Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I can
perform the string comparison?

Thanks
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
If i understand you correctly i would do it like this:

StreamReader f = new StreamReader(fileName);
ArrayList lines = new ArrayList();

string line;

while ((line = f.ReadLine()) != null )

{

lines.Add(line);

}

f.Close();

Then you move to the last index in ArrayList and tests for the word DONE

Best regads

Trond

"Tarren" <no***********@thanks.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I
can perform the string comparison?

Thanks

Nov 17 '05 #2

P: n/a
Hi Tarren,

Besides reading all the info like Trond said, if you know that there is nothing after DONE (like line breaks or tabs etc) you could set the StreamReader's position to last position -4

using(FileStream fs = File.OpenRead("c:\\file.dat"))
{
using(StreamReader sr = new StreamReader(fs))
{
sr.BaseStream.Position = fs.Length - 4;
if(sr.ReadToEnd() == "DONE")
// match
}
}
On Mon, 25 Apr 2005 17:00:01 +0200, Tarren <no***********@thanks.com> wrote:
Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I can
perform the string comparison?

Thanks


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.