It's generally not possible to read a file stream backwards, you can however store the results in a list and traverse through the list from last element to first. (It also might be a good idea to save the absolute position of the EOF and start reading from that position when you read the log file again to check if it changes. I'd have to look up how is this done in C#).
String class in C# provides a Split() method which splits a string into multiple strings separated by a given delimiter. For instance, calling Split("_") on string "Hello,_World!" will return strings "Hello," and "World!". There is also an IndexOf() method which returns the index of a given substring in a string. Calling IndexOf(",_") on the previous string will return 5.
EDIT: It is perfectly possible to read a file backwards because file is nothing but an array of bytes. Reading streams backwards is what cannot be done, because while you're reading the beginning of the stream, the end of it might not yet be available (think Youtube videos). With that in mind you could probably access the log file in a binary mode, look for EOLs and create strings from segments between two EOLs.
Oct 04 Authorised : ip (name)
Oct 06 Authorised : ip (name)
The problem was that it was reading always the first line which could've been outdated , But you gave me an idea.
I stored the first line in a string (ex: Pinfo) , and then used statement If (read line != Pinfo) // != means the opposite of == , which can also be as (read line == Pinfo == false) {
read the line
}
So now its always reading the last available line
Oh and about the extracting text
the line : Oct 03 13:03:02 Authorised : 99.99.99.99 (username)
int pFrom = info.IndexOf(": ") + ": ".Length;
int pTo = info.LastIndexOf(" (");
string result = info.Substring(pFrom, pTo - pFrom);
Sorry for the late answer,
But i have added the last 3 lines under "while" and met the same BUG, about the first 2 lines, im sure theirs place is right, and got that working on other stats.
Your code doesn't do a lot of things it should. It doesn't check if the file you're trying to open exists and that it's readable. It assumes that the characters you're searching for are always there. It also uses something called "UserInfo" which seems to be a global variable - that's usually a bad practice. There are many reasons why a code like this would break. Also, comparing string contents with "==" operator doesn't do what you want in C#. Use String.Equals() instead.
Dude, I used the exact code but without Username check, and it worked and gave me the IP, if you read my post above, i asked why it keeps reading the first line, because i figured out how to get the IP, but didn't know why it keeps reading the first line, so when i was waiting an answer, i added that new code to check the username before getting the IP, and it BUGGED the whole packet, and i came here with another error.
About UserInfo, it's a file where i have player's stats, and inside that folder i created IP.txt, and wrote there some lines, here are they:
LOL
Oct 03 13:03:02 Authorised : 99.99.99.99 (blabla)
Oct 04 14:04:03 Authorised : 99.99.99.90 (ablabl)
Oct 05 15:05:04 Authorised : 99.99.90.99 (glagla)
that's it.
I hope you understand what i meant.
Thanks a lot for your time.
Yes it does. String is a special case where == is overloaded to compare value instead of reference. (MSDN)
A couple of issues with the code:
1.
All cases of
return "";
within the while loop should be
continue;
If you use
return "";
it will exit the function at the first sniff of a line that doesn't contain an IP - if you're scanning a log file, that means it'll quit on line 1. Continue will instead move on to the next line, which is probably what you want.
2.
FromIdx = Line.IndexOf(" (") + " (".Length;
and
FromIdx = Line.IndexOf(": ") + ": ".Length;
will return 1 instead of the expected -1 if the string is not found. You need to check the result of .IndexOf() before adding anything to FromIdx:
3.
If anyone types ": blub (Somebody'sUsername)" into chat, it won't return what you're looking for.
P.S.
You're probably better off doing this kind of thing using Regex. It'll be more reliable, probably faster and can be done in something like 10 or so lines of code.
P.P.S
Also, I just realised that code will still only find the first IP for that user, not the latest.
You'll need to define IPStr at the start of the function, keep running the while loop until you hit EOF, then return the latest version of IPStr.