0

Currently I am building an agenda with extra options.

for testing purposes I store the data in a simple .txt file (after that it will be connected to the agenda of a virtual assistant.)

To change or delete text from this .txt file I have a problem. Although the part of the content that needs to be replaced and the search string are exactly the same it doesn't replace the text in content.

code:

Change method

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

replace method

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

When running in debug I get the correct info:

    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

But it doesn't change the text. How do I make sure that the Regex.Replace finds the right piece of content?

PS. I did check several topics on this, but none of the solutions mentioned there work for me.

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 に答える