简体   繁体   中英

Removing a line from a text file in C#

i am a newbie in C# and i have a simple console application with method validateVoters() which takes a studentID argument, compares it against text file then return appropriate boolean value. However i want it to delete that specific studentID if it exists then return true, but there is no generic delete from file method so i used a method recommended by a member here: Giving me an error with the method in double asterics ** :
Error 2
The name 'RemoveUnnecessaryLine' does not exist in the current context 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"));
    }

   }
}
/* 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);
}

Try this:

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

It will read the file, and when a match is found, then it shifts the remaining lines up one line. The last line will be cleared, then the file gets written back to disk. If you do not want to have an empty last line, then you can resize the array (see Array.Resize ).

Try using 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM