从C#中的文本文件中删除一行

时间:2013-10-05 01:32:33

标签: c# file-io

我是C#的新手,我有一个方法 validateVoters()的简单控制台应用程序,它接受一个studentID参数,将它与文本文件进行比较,然后返回适当的布尔值。 但是我希望它删除该特定studentID,如果它存在然后返回true,但是没有从文件方法删除泛型,所以我使用了一个成员推荐的方法: 在双星号中给我一个错误的方法**:
错误2
当前上下文中不存在名称“RemoveUnnecessaryLine”c:\ Users \ Hlogoyatau \ Documents \ Visual Studio 2010 \ Projects \ Ijoo \ Ijoo \ Program.cs 28 43 Ijoo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SRCVotingSystem
 {

public class Program
{


    public bool validateVoter(String cisNo)
    {
        bool found = false;

        try
        {
            string[] ID = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt");

            foreach (string line in ID)
            {
                //compares it against text file contents
                if (cisNo == line)
                {
                    string[] allLines= File.ReadAllLines("votersRoll.txt");
                    string[] newIDs= **RemoveUnnecessaryLine**(allLines);
                    File.WriteAllLines("votersRoll.txt", newIDs);


                    found = true;
                }
            }
        }

        catch (IOException e)
        {
            Console.WriteLine(e.ToString());
        }

        return found;
    }


    public static void Main()
    {

        Program vv = new Program();
        Console.WriteLine(vv.validateVoter("cis11-005"));
    }

   }
}

3 个答案:

答案 0 :(得分:2)

/* sample data in text.tx
 ID 1 asdfsdaf
 ID 2 asdfdsafasdfsadf
 ID 3 lkjasdfjsdf
*/
private static void Main(string[] args)
{
    var id = 2;
    var lines = File.ReadAllLines("C:\\temp\\text.txt");

    var remaining = lines.Where(x => !x.Contains(id.ToString())).ToArray();
    File.WriteAllLines("C:\\temp\\out.txt", remaining);
}

答案 1 :(得分:0)

试试这个:

public bool validateVoter(String cisNo)
{
    bool found = false;

    try
    {
        string[] ID = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt");

        for (int i = 0; i < ID.Length; i++)
        {
            string line = ID[i];
            //compares it against text file contents
            if (cisNo == line)
            {
                //Shift remaining lines up, overwriting current line
                for (int j = i; j < ID.Length - 1; j++)
                {
                     ID[j] = ID[j+1];
                }

                //Set last line to empty string
                ID[ID.Length - 1] = "";

                //Write file back to disk
                System.IO.File.WriteAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt", ID);

                found = true;

                //Exit loop after something is found
                break;
            }
        }
    }

    catch (IOException e)
    {
        Console.WriteLine(e.ToString());
    }

    return found;
}

它将读取文件,当找到匹配项时,它会将剩余的行向上移动一行。最后一行将被清除,然后文件将被写回磁盘。如果您不想要一个空的最后一行,那么您可以调整数组的大小(参见Array.Resize)。

答案 2 :(得分:0)

尝试使用LINQ

public void validateVoter(String cisNo)
{
    var newIDs = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt").Where(l => l != cisNo);
    File.WriteAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt", newIDs);
}