views:

646

answers:

2

I require help to search a text file (log file) using c# and display the line number and the complete line that contains the search keyword.

+1  A: 

This is a slight modification from: http://msdn.microsoft.com/en-us/library/aa287535%28VS.71%29.aspx

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
    if ( line.Contains("word") )
    {
        Console.WriteLine (counter.ToString() + ": " + line);
    }

   counter++;
}

file.Close();
Pessimist
thanks optimist :)
Chitresh
Now can you guide me how can i export the same to excel.i.e. line and line number
Chitresh
Hm... you want the line number in one cell, and the actual line in another? If so, instead of separating by the colon there (on ": "), separate them by comma (",") and then have Excel read that as a CSV file (comma-separated value).
Pessimist
no i need to built the functionality in the app itself to be able to export to excel... (requirements are such) .. please help
Chitresh
That I don't know how to do. I'd search the web for c# export excel or I'd open a new question about that.
Pessimist
ok thanks.. please keep me updated on this...
Chitresh
A: 

i have a project: " create index index on a text file, for creating an index, the user will be requried to specify the name of the file on which the index needs to be created and the name of the index file. if index has already been created on a file, a user should not be required to create it again the next time an operation is to performed on the file.. and load the files by index file i'm learn programming in C#, 3 months ago this project required write in windowapplication. help me! thanks so much

nguyen phuoc duy