this simply writes the text in textbox to the same file each time…I don’t understand why it works perfectly if I input new characters or change characters while deleting characters doesn’t work…
private void ContentChanged(object sender, TextChangedEventArgs e)
{
Console.WriteLine("cur before:" + this.Box.SelectionStart);
FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(f);
model.Cursor = this.Box.SelectionStart;
writer.Write(this.Box.Text);
writer.Close();
Console.WriteLine("cur after:" + this.Box.SelectionStart);
Console.WriteLine("write:" + count++);
Console.WriteLine("after write:" + this.Box.Text);
Console.WriteLine("after write:" + model.Content);
}
You’re not closing
for flushing – it’s entirely possible that the data is being buffered, which is why it never makes it to the file.Two suggestions:
usingstatements with disposable resources such as streams and writersFile.CreateTextto create aTextWriterin one call – or better yet, useFile.WriteAllTextto avoid having to worry about opening and closing at all.So something like this: