Search code examples
c#stringtext-filesstreamreaderstreamwriter

Writing List<String> contents to text file after deleting string


I'm trying to get the contents of a Text File, delete a line of string, and re-write back to the Text File, deleting the line of string. I'm using StreamReader to get the text, importing into a List, removing the string, then rewriting using StreamWriter. My problems arises somewhere around the removing or writing of the string. Instead of writing back the existing, non deleted contents to the text file, all the text is replaced with :

System.Collections.Generic.List`1[System.String]

My code for this function is as follows:

{
            for (int i = deleteDevice.Count - 1; i >= 0; i--)
            {
                string split = "";
                //deleteDevice[i].Split(',').ToString();
                List<string> parts = split.Split(',').ToList();

                if (parts.Contains(deviceList.SelectedItem.ToString()))
                {

                    deleteDevice.Remove(i.ToString());
                }

            }
            if (deleteDevice.Count != 0) //Error Handling
            {

                writer.WriteLine(deleteDevice);

            }
        }

        deviceList.Items.Remove(deviceList.SelectedItem);
    }

I would just like the script to write back any string that isn't deleted (If there is any), without replacing it. Any help is appreciated, Cheers


Solution

  • You can read all the info from the text file into a list and then remove from the list and rewrite that to the text file.

    I would change the list 'deleteDevice' to store a string array instead and use the code below to determine which item to remove.

            List<int> toRemove = new List<int>();
            int i = 0;
            /*build a list of indexes to remove*/
            foreach (string[] x in deleteDevice)
            {
                if (x[0].Contains(deviceList.SelectedItem.ToString()))
                {
                    toRemove.Add(i);
                }
                i++;
            }
    
            /*Remove items from list*/
            foreach (int fd in toRemove)
                 deleteDevice.RemoveAt(fd);
    
            /*write to text file*/
            using (StreamWriter writer = new StreamWriter("Devices.txt"))
            {
                if (deleteDevice.Count != 0) //Error Handling
                {
                    foreach (string[] s in deleteDevice)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int fds = 0; fds < s.Length; fds++ )
                        {
                           sb.Append(s[fds] + ",");
                        }
                        string line = sb.ToString();
                        writer.WriteLine(line.Substring(0, line.Length - 1));
                    }
                }
            }
    

    This isn't the best solution but should work for your needs. There's probably a much easier way of doing this.