0

我是 C# 的新手,我有一个简单的控制台应用程序,它带有方法validateVoters(),它接受一个 studentID 参数,将它与文本文件进行比较,然后返回适当的布尔值。但是,我希望它删除该特定的学生ID(如果存在)然后返回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"));
    }

   }
}
4

3 回答 3

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);
}
于 2013-10-05T01:56:10.253 回答
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)。

于 2013-10-05T01:44:48.643 回答
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);
}
于 2013-10-05T01:55:07.510 回答