-1

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

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

我的这个函数的代码如下:

{
            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);
    }

我只想让脚本写回任何未删除的字符串(如果有的话),而不用替换它。任何帮助表示赞赏,干杯

4

3 回答 3

2

您可以将文本文件中的所有信息读入列表中,然后从列表中删除并将其重写到文本文件中。

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

        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));
                }
            }
        }

这不是最好的解决方案,但应该可以满足您的需求。可能有一种更简单的方法可以做到这一点。

于 2013-10-24T22:59:18.477 回答
1

问题

deleteDevice是 type List<string>,并且因为它也不重载ToString(),所以默认行为List<string>.ToString()是返回类型的名称。

因此,您的行writer.WriteLine(deleteDevice);写入字符串System.Collections.Generic.List1[System.String]`。

除此之外,您的代码还有很多问题......

例如,您这样做:

string split = "";

然后在线上执行以下操作:

List<string> parts = split.Split(',').ToList();

但是因为split是 "",所以这总是会返回一个空列表。


解决方案

为了简化代码,您可以首先编写一个辅助方法,该方法将从文件中删除与指定谓词匹配的所有行:

public void RemoveUnwantedLines(string filename, Predicate<string> unwanted)
{
    var lines = File.ReadAllLines(filename);
    File.WriteAllLines(filename, lines.Where(line => !unwanted(line)));
}

然后你可以写这样的谓词(这可能不太正确;我真的不知道你的代码在做什么,因为它不可编译并且省略了一些类型):

string filename = "My Filename";
string deviceToRemove= deviceList.SelectedItem.ToString();

Predicate<string> unwanted = line => 
    line.Split(new [] {','})
    .Contains(deviceToRemove);

RemoveUnwantedLines(filename, unwanted);
于 2013-10-24T08:28:46.497 回答
1

问题出在以下行:

writer.WriteLine(deleteDevice);

您正在编写 deleteDevice(我假设这是 List 类型)。List.ToString() 返回列表的类型名称,因为这没有具体的实现。你想要的是

foreach(String s in deleteDevice)
{
    writer.WriteLine(s);
}
于 2013-10-24T08:29:14.033 回答