Search code examples
c#asp.netarraysstringfileinputstream

Deleting current string array from a file C# asp.net


Just a simple newbie question. I want to delete the displayed string in an "accepted" button. Once the "accepted" button is clicked the displayed array is written in another file, however I need the displayed one line array to be deleted from this file. Thanks for your time. Much appreciated!!!

Below is my code:

    StreamReader srAc = new StreamReader(Server.MapPath("~") + "\\App_Data\\UserEntryNew.txt");

    string allAccept = srAc.ReadToEnd();

    string[] allAcceptArray = allAccept.Split('\n');

    string accepted = "";

    for (int i = 0; i < allAcceptArray.Length; i++)
    {
        if (allAcceptArray[i] == modTextBox.Text)
        {
                accepted = allAcceptArray[i];
        }
    }

Solution

  • You can use linq to remove the entry from the array. Assuming you have an array allAcceptArray and you want to remove modTextBox.Text from the array:

     allAcceptArray = allAcceptArray.Where(x => x != modTextBox.Text).ToArray();