[RESOLVED] Read Specific Line from File
Hello all,
I spent about 2 hours searching for an answer to this on the web and could not find anything that gave me a clear answer.
Currently I read in a file as a TEXT file in my program. I take the file and I do a line count on it to determine how many lines are in it. What I need to do is use that line count to read the LAST line in the file (I also use it to determine how the program should continue).
Here is what I use to determine line count; it works fine as the size of the files will ALWAYS be small.
Code:
int LineCount = File.ReadAllLines(MajorRev).Length
What I would like to do is then read the line number of the file based on LineCount but I can't figure out or find a way to read the last line based on this...
Any help is creatly appreciated. Thanks! :)
Re: Read Specific Line from File
the ReadAllLines method returns a string array, so instead you should:
string[] sLines = File.ReadAllLines(MajorRev);
string sLastLine = sLines[sLines.Length - 1];
Re: Read Specific Line from File
Sweet! Thanks for the information, that worked! I figured there had to be an easy way to do it.
Re: [RESOLVED] Read Specific Line from File
(As a side note, you should probably make sure .Length isn't 0 - IE an empty file before you attempt to read the last line)
Re: [RESOLVED] Read Specific Line from File
I normally would agree with you... I have error checking earlier in the application to see if the file exists and the only way the file can exist for the program that uses it (not my application) is if it has information in it. The software doesn't let the file be blank.
I do have another question regarding this same application I am working on though; maybe you can help.
The file thats read will have one of two things.
1. A list that is user defined; something like A, B, C, D or A1, A2, A3, A4...
2. A range that is defined; something like A-Z or 00-99...
If it is option 2 the file will only have 2 lines in it. If it is option one it has a line for each of the values in the list. What I need to do is first determine which it is (done with a last line count) then based on the information in the file I need to search for a folder.
So lets say I find out that its option 1. And the list is A, B, C & D. I then need to query a subdirectory for the highest of these (D is higher than A in this case). So if the folder I query only has B I need to return B as it is the latest.
Any suggestions on the best method?
Re: [RESOLVED] Read Specific Line from File