2
if (intBestNumberOfTries > intNumberOfTries)
{
    string searchFor = LoginForm.strLoginName;
    string line;
    using (StreamReader file = new StreamReader(@"NameList.txt"))
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(searchFor))
            {
                line.Remove(0);
            }
        }
    }
}
StreamWriter scoreboard = new StreamWriter(@"NameList.txt", true);
scoreboard.WriteLine(""[{0}] " + LoginForm.strLoginName + " scored: {1}", intBestNumberOfTries, intBestNumberOfTries);
scoreboard.Close();

My Loginform.strLoginName; comes from another programming file. After I run the program, it does not remove the whole line. Instead, it writes a new line. searchFor is my login name into the program.

Sentry
  • 4,102
  • 2
  • 30
  • 38
Kimber Ker
  • 29
  • 6

3 Answers3

3

This cannot be done in the way you're providing. You need to rewrite the file.

How ?

You have to read the file into memory structure, remove the line in memory and then put the contents back to the file (it's just overwriting).

If you handle a file which is large or very large you have to read it line by line and creating a temp file(as helper) and then just replace the original one.

However, if the file is large you can use an asynchronous operation in order to not block the current thread. c# StreamReader function provided a solution for this using StreamReader.ReadToEndAsync method.

Now, for your problem, you should use File.ReadAllLines method which returns an array of strings.

string[] lines = File.ReadAllLines("NameList.txt");

The last step is to filter this array.

string[] filteredLines = lines.Where(l => !line.Contains(searchFor)).ToArray()
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
2

Strings are immutable, that's why every string.method like String.Remove will return a new string. You have to assign this returned string to your variable. For example: line = line.Remove(0). Although i think you want something completely different(Remove(0) will return an empty string). Read the documentation of the methods to see what they are doing.

If you want to remove this line from the file you have to rewrite it with the lines you want to keep::

string[] allLines = File.ReadAllLines("NameList.txt");
string[] keepLines = Array.FindAll(allLines, line => !line.Contains(searchFor));
File.WriteAllLines("NameList.txt", keepLines);

or use LINQ (ReadLines uses a streaming approach):

var allLines = File.ReadLines("NameList.txt");
var keepLines = allLines.Where(line => !line.Contains(searchFor));
File.WriteAllLines("NameList.txt", keepLines.ToList());
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Do as following , You need to overwrite

        string tempFile = Path.GetTempFileName();

        using (var sr = new StreamReader("NameList.txt"))
        using (var sw = new StreamWriter(tempFile))
        {
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                if (line.Contains(searchFor))
                    sw.WriteLine(line);
            }
        }

        File.Delete("NameList.txt");
        File.Move(tempFile, "NameList.txt");

You can also achieve this with Linq

var tempFile = Path.GetTempFileName();
var linesToKeep = File.ReadLines(fileName).Where(l => l.Contains(searchFor));

File.WriteAllLines(tempFile, linesToKeep);

File.Delete(fileName);
File.Move(tempFile, fileName);

If you are not deal with large files , then you can get rid from Temp file

File.WriteAllLines(fileName, 
File.ReadLines(fileName).Where(l => !l.Contains(searchFor)).ToList());
Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20