写入列表<字符串>删除字符串后内容到文本文件 [英] Writing List&lt;String&gt; contents to text file after deleting string

查看:36
本文介绍了写入列表<字符串>删除字符串后内容到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取文本文件的内容,删除一行字符串,然后重新写回文本文件,删除该行字符串.我正在使用 StreamReader 获取文本,导入到列表中,删除字符串,然后使用 StreamWriter 重写.我的问题出现在删除或写入字符串的某个地方.不是将现有的未删除内容写回文本文件,而是将所有文本替换为:

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]

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

推荐答案

您可以将文本文件中的所有信息读入一个列表,然后从列表中删除并重新写入文本文件.

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.

我会更改列表 'deleteDevice' 以存储字符串数组,并使用下面的代码来确定要删除的项目.

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.

这篇关于写入列表<字符串>删除字符串后内容到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆