Question

I'm building an app for windows 8 desktop, I'm reading in a text file and I want to change one specific line but not sure how so what I have is a text file that says

username|false
username|false
username|false

And I want to remove the middle line when something happens, this is what I have so far;

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile storageFile = await folder.GetFileAsync("students.txt");
var text = await Windows.Storage.FileIO.ReadLinesAsync(storageFile);
var list_false = "";

foreach (var line in text)
{
    string name = "" + line.Split('|')[0];
    string testTaken = "" + line.Split('|')[1];
    if (your_name.Text == name)
    {
        if (testTaken == "false") {
            pageTitle.Text = name;
            enter_name_grid.Opacity = 0;
            questions_grid.Opacity = 1;
            var md = new MessageDialog("Enjoy the test");
            await md.ShowAsync();
        }
        else
        {
            the_name.Text = "You have already taken the test";
            var md1 = new MessageDialog("You have already taken the test");
            await md1.ShowAsync();
        }
        return;
    }
    else
    {
        list_false = "You're not on the list";
    }
}
if (list_false == "You're not on the list") {
    var md2 = new MessageDialog("You're not on the list");
    await md2.ShowAsync();
}

Help please, it reads in names perfectly and allows them to take the test, I just need it to remove the correct line. Thanks in advance!!

Was it helpful?

Solution

The important thing to consider is that you are modifying a file. So whatever you choose to change then you need to write it back to the file.

In your case you are opting to read the whole file into memory, this actually works in your favor for something like this as you can just remove any unwanted lines and write back to the file. However, you cannot remove an item while you are iterating through the list using a foreach loop.

The best practice for removing items from an array you are looping is to use a for loop and loop in reverse. It also makes it easier to remove items if we work with a List<string> too, like so:

var list = new List<string>(text);
for(int i = text.Length - 1; i >=0; i--)
{
    string line = text[i];
    //rest of code
}
text = list.ToArray();

The next part of your task is to remove the line. You can do this in your else statement as this is the part that handles users already having taken the test. For example:

the_name.Text = "You have already taken the test";
list.RemoveAt(i);

Finally, after your loop you need to write the whole thing back to the file:

await Windows.Storage.FileIO.WriteLinesAsync(storageFile, text);

OTHER TIPS

When you read the file, you could store the contents in a list. When your "something happens" you could remove the content at the appropriate index and save (overwrite) the list to the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top