C#如何根据行号删除特定行。从文本文件 [英] C# How to delete specific lines based on line no. from a text file

查看:138
本文介绍了C#如何根据行号删除特定行。从文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,

我陷入了一个小而简单的剪切日志文件内容的问题。这是紧急的!



问题:

我需要C#(2.0 framewrk)中的代码,我可以根据行号从日志文件中删除特定行。我在网上搜索但是到处都是基于C#4.0框架或基于System.Linq的API获取solns,我无法使用。此外,我找到了解决方案,如创建一个编辑的临时文件,然后用这个临时文件替换原始日志文件。这个溶点。我也无法使用,因为如果在编辑和替换过程中,如果用户已经在其他编辑器中打开了日志文件,那么编辑和替换过程就会失败并抛出错误该文件正由另一个进程使用。



所以,我只是希望你的帮助知道我是否可以立即使用其他任何命令或API。我试图使用writeline,但它在前一个日志的末尾添加了所需的行,它没有替换''not reqd''行。



请帮忙,紧急。

谢谢。

Dinesh

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

推荐答案

IT非常简单。将文件读入String数组:

http://msdn.microsoft。 com / en-us / library / s2tte0y1.aspx [ ^ ]



对阵列进行修改,意味着RemoveAt命令:

http://msdn.microsoft.com/en-us/library/ms132414.aspx [ ^ ]



并编写新的模拟数组:

http:// msdn。 microsoft.com/en-us/library/3det53xh.aspx [ ^ ]
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[^]


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();
           }

       }







来电read方法并传递行号




call the read method and pass the line number


这篇关于C#如何根据行号删除特定行。从文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆