Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...