且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除csv文件中的行

更新时间:2023-12-03 21:26:22

如果修复了某些集合,请不要尝试删除该行。从文件填充数据时跳过不需要的行。您可以逐行阅读,而不是使用 ReadAllLines ,而在每个类似的情况下,您可以决定是否要跳过它。对于您填充的集合,请使用 System.Collections.Generic.List< string> 。而不是文件,使用类 System.IO.StreamReader

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



它可能是这样的:

If some collection is fixed, don't try to delete the row. Skip unwanted row when you populate the data from file. Instead of using ReadAllLines, you could read line by line and, on each like, decide if you want to skip it. For the collection you populate, use System.Collections.Generic.List<string>. Instead of File, use the class System.IO.StreamReader:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].

It could be something like:
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName) {
    string line = reader.ReadLine();
    if (line /* if something's wrong with this line... */)
        continue;
    list.Add(line);
    // ... whatever else you want to do
} // reader.Dispose is automatically called on exit from this block



顺便说一下,这种类型的列表将允许你删除项目(行),但要小心。 br $>


-SA