Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,
I am stuck in a small & simple problem of clipping a log file content.And this is URGENT!!

Problem:
I need a code in C# (2.0 framewrk) by which i can delete specific lines from a log file based on line number. I searched over net but everywhere i am getting solns based on C#4.0 framework or System.Linq based APIs, which i can't use. Also i found solutions like creating a edited temp file and then in the end replacing the original log file with this temp file. This soln. also i cant use because if during this editing & replacing process, if user had already opened the log file in some other editor then the editing & replacing process fails throwing an error "The file is in use by another process".

So, i just want your help to know if there are any other commands or APIs which i can straight away use. i tried to use writeline, but its adding the required lines in the end of previous log, its not replacing the 'not reqd' lines.

Please help, its urgent.
Thank you.
Dinesh
Posted

ITs pretty simple. Read the file into a String array:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx[^]

Do the modifications on the array, meaning RemoveAt command:
http://msdn.microsoft.com/en-us/library/ms132414.aspx[^]

And Write the newly modefied array:
http://msdn.microsoft.com/en-us/library/3det53xh.aspx[^]
 
Share this answer
 
Comments
[no name] 18-Apr-13 5:23am    
Thanks Kenneth, but the RemoveAt command is not available in my framework. :(
You have any other idea?
Kenneth Haugland 18-Apr-13 5:26am    
do a for loop with the read array than, and skip the line you dont want.
[no name] 18-Apr-13 5:33am    
Kenneth, Thanks for the tip. It will be a big help if u can paste a code snippet..I know its too much to ask but i need it urgently..
Kenneth Haugland 18-Apr-13 5:59am    
http://msdn.microsoft.com/en-us/library/ch45axte.aspx
C#
public void read(int lineNumber)
       {
           int counter = 0;
           string line;
           System.IO.StreamReader file =new System.IO.StreamReader("D:\\test1.txt");
           string newstr = null;
           while ((line = file.ReadLine()) != null)
           {
               // Console.WriteLine(line);
               if ((counter + 1).Equals(lineNumber))
               {

               }
               else
               {
                   newstr += line + Environment.NewLine;

               }
               counter++;
           }

           file.Close();

           if (System.IO.File.Exists("D:\\test1.txt"))
           {
               System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\test1.txt");
               sw.WriteLine(newstr);
               sw.Close();
           }

       }



call the read method and pass the line number
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900