0

目前我正在建立一个带有额外选项的议程。

出于测试目的,我将数据存储在一个简单的 .txt 文件中(之后它将连接到虚拟助手的议程。)

要更改或删除此 .txt 文件中的文本,我遇到了问题。尽管需要替换的部分内容和搜索字符串完全相同,但它不会替换内容中的文本。

代码:

改变方法

public override void Change(List<object> oldData, List<object> newData)
    {
        int index = -1;
        for (int i = 0; i < agenda.Count; i++)
        {
            if(agenda[i].GetType() == "Task")
            {
                Task t = (Task)agenda[i];

                if(t.remarks == oldData[0].ToString() && t.datetime == (DateTime)oldData[1] && t.reminders == oldData[2])
                {
                    index = i;
                    break;
                }
            }
        }

        string search = "Task\r\nTo do: " + oldData[0].ToString() + "\r\nDateTime: " + (DateTime)oldData[1] + "\r\n";
        reminders = (Dictionary<DateTime, bool>) oldData[2];
        if(reminders.Count != 0)
        {
            search += "Reminders\r\n";

            foreach (KeyValuePair<DateTime, bool> rem in reminders)
            {
                if (rem.Value)
                    search += "speak " + rem.Key + "\r\n";
                else
                    search += rem.Key + "\r\n";
            }
        }

        // get new data
        string newRemarks = (string)newData[0];
        DateTime newDateTime = (DateTime)newData[1];
        Dictionary<DateTime, bool> newReminders = (Dictionary<DateTime, bool>)newData[2];

        string replace = "Task\r\nTo do: " + newRemarks + "\r\nDateTime: " + newDateTime + "\r\n";
        if(newReminders.Count != 0)
        {
            replace += "Reminders\r\n";
            foreach (KeyValuePair<DateTime, bool> rem in newReminders)
            {
                if (rem.Value)
                    replace += "speak " + rem.Key + "\r\n";
                else
                    replace += rem.Key + "\r\n";
            }
        }

        Replace(search, replace);

        if (index != -1)
        {
            remarks = newRemarks;
            datetime = newDateTime;
            reminders = newReminders;
            agenda[index] = this;
        }
    }

替换方法

    private void Replace(string search, string replace)
    {
        StreamReader reader = new StreamReader(path);
        string content = reader.ReadToEnd();
        reader.Close();

        content = Regex.Replace(content, search, replace);
        content.Trim();

        StreamWriter writer = new StreamWriter(path);
        writer.Write(content);
        writer.Close();
    }

在调试中运行时,我得到正确的信息:

    content "-- agenda --\r\n\r\nTask\r\nTo do: test\r\nDateTime: 16-4-2012 15:00:00\r\nReminders:\r\nspeak 16-4-2012 13:00:00\r\n16-4-2012 13:30:00\r\n\r\nTask\r\nTo do: testing\r\nDateTime: 16-4-2012 9:00:00\r\nReminders:\r\nspeak 16-4-2012 8:00:00\r\n\r\nTask\r\nTo do: aaargh\r\nDateTime: 18-4-2012 12:00:00\r\nReminders:\r\n18-4-2012 11:00:00\r\n"    string

    search  "Task\r\nTo do: aaargh\r\nDateTime: 18-4-2012 12:00:00\r\nReminders\r\n18-4-2012 11:00:00\r\n"  string

    replace "Task\r\nTo do: aaargh\r\nDateTime: 18-4-2012 13:00:00\r\nReminders\r\n18-4-2012 11:00:00\r\n"  string

但它不会改变文本。如何确保 Regex.Replace 找到正确的内容?

PS。我确实检查了几个主题,但那里提到的解决方案都不适合我。

4

2 回答 2

0

您可以尝试使用 StringBuilder 来构建您想要写入文件的内容。

刚刚在控制台应用程序中敲了一个简单的例子,但这似乎对我有用,我认为这可能是你正在寻找的。

        StringBuilder sb = new StringBuilder();

        sb.Append("Tasks\r\n");
        sb.Append("\r\n");
        sb.Append("\tTask 1 details");

        Console.WriteLine(sb.ToString());

        StreamWriter writer = new StreamWriter("Tasks.txt");
        writer.Write(sb.ToString());
        writer.Close();
于 2012-04-18T10:42:00.707 回答
0

你错过了一个:之后Reminders。再检查一遍:)

于 2012-04-18T10:42:33.463 回答