从C#中的文本文件中删除特定行(查看详细信息) [英] Delete Specific Line from Text File in C# (See details)

查看:151
本文介绍了从C#中的文本文件中删除特定行(查看详细信息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Windows窗体应用程序。

文本文件存储一些带代码和索引的名称(如:Chris; 987; 5)。

然后使用该文本文件进行读取。 />


现在,我有2个文本框(userTXT和codeTXT)和一个按钮(removeBtn)。

用户必须在<$ c中写入其用户名$ c> userTXT 以及在 codeTXT 中创建帐户时由同一应用程序提供的代码。按下按钮时,程序应读取文本文件(data.txt),删除包含匹配用户名和代码的行,并更新一个datagridview,其中每个用户名都作为行标题单元格。



问题:



我已尝试多种方法删除txt文件中的那一行(包括:制作第二个实例)该文件,删除该行并删除原始文件;使用 while 函数,将文件读取到列表并使用删除或删除at),但这些中没有一个工作。按下按钮后,文本文件看起来与尝试删除行之前相同。尝试调试,没有错误,尝试断点,我没有发现任何重要的事情。



请帮帮我;在此提前感谢 - CCB

I`m writing a Windows Form application.
A text file is storing some names with codes and an index(like: Chris;987;5).
That text file is used then for reading it.

Now, I have 2 textboxes( userTXT and codeTXT) and a button (removeBtn).
A user must write its username in userTXT and a code that is given by the same application when making an "account" in codeTXT. When the button is pressed, the program should read the text file (data.txt), remove the line that contains the matching username and code and update the a datagridview which has each username as a row header cell.

The problem:

I`ve tried many ways to delete that line in the txt file (including: making a second instance of the file, removing the line and deleting the original file; using While function, reading the file to a list and use remove or remove at), but no one of these works. After the button is pressed, the text file looks the same as before trying to delete the line. Tried debugging and no errors, tried breakpoints and I didn`t find anything significant.

Please help me; Thanks in Advance - CCB

推荐答案

逐行读取文件并逐行将结果写入不同的文件中,跳过所需的行。使用这两个类:

https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en -us / library / system.io.streamwriter%28v = vs.110%29.aspx [ ^ ]。



输出文件,使用临时文件名: https:// msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx [ ^ ]。



它完成后,将临时文件重命名为原始文件,如果这是你需要的。具体方法如下: https:// msdn。 microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx [ ^ ]。



不要忘记关闭所有流,否则文件将无法访问。最好的方法是调用 IDisposable.Dispose 读者和作者的方法,反过来,最好的方法是使用使用 声明(不要与使用 指令混淆):

https://msdn.microsoft.com/en-us/library/ yh598w02.aspx [ ^ ],

https:// msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx [ ^ ]。



这是一种非常通用的方法,适用于任何大小的文件。



现在,对于较小的文件,这是轻量级方法:读取所有行一旦到内存,处理行以忽略不需要的行,将所有行写入文件:

https://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.io.file.writealllines%28v=vs.110%29.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.io.file.writealllines%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



-SA
Read the file line by line and write the result in a different file line by line, skipping the required lines. Use these two classes:
https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx[^].

For output file, use temporary file name: https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^].

When it is done, rename temporary file to the original file, if this is what you need. This is how: https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx[^].

Don't forget to close all streams, otherwise the files will remain inaccessible. The best way to do it is to call IDisposable.Dispose methods of the reader and the writer, and, in turn, the best way to do it is to use using statement (not to be mixed up with using directive):
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

This is a very universal approach which can work for the files of any size.

Now, this is light-weight approach for smaller files: read all lines at once to memory, process the lines to ignore unwanted lines, write all lines to the file:
https://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.file.writealllines%28v=vs.110%29.aspx[^].

—SA


这篇关于从C#中的文本文件中删除特定行(查看详细信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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