textfile reading

laxman

Active member
Joined
Jun 14, 2008
Messages
26
hai all

i am developing a windows application, in this application i am creating log file(textfile), here i want to generate the report based on the log file i am able to read full data but i want to capture the line which contains specifik key word here i am unable get the line which contains the specific key word let me know how to do this

thanks in advance
 
You would have to read through the file a line at a time and check each line for the keyword in question.

Alternatively you could read the entire file into memory and split it into an array of strings then loop over the array looking for the keyword.
 
Code:
String[] lines = System.IO.File.ReadAllLines("YourFilePath.log");
foreach (String line in lines)
{
    if (line.Contains(yourKeyWord) == true)
    {
        //do work
    }
}

This could also be done without the extra variable, but the above helps illustrate what is happening.

Code:
foreach (String line in System.IO.File.ReadAllLines("YourFilePath.log"))
{
    if (line.Contains(yourKeyWord) == true)
    {
        //do work
    }
}
 
thank you for your reply and i am able to get the data by using contains key word.here my problem is i am updating each and every time if user does any thing so if suddenly power off shut down, after restarting i have to get the last six lines or last say some half an hour data in the file if any one suggest with one example it would be great thank you
 
You could open the file using a filestream and seek to a couple of k from the end, then open a streamreader on top of the filestream and read from there.
 

Similar threads

I
Replies
0
Views
47
It_s Meee
I
S
Replies
0
Views
46
sva0008
S
C
Replies
0
Views
58
Cas Raj
C
Back
Top