I'll try my best to explain my situation.
I'm developing a software using C#, which allows multiple users to edit the same file under a common directory at the same time and see the changes others make too.
So I used FileSystemWatcher to monitor the changes in the file (to update others' changes) and textchanged of the textbox (to save changes to the file so others' screen would be updated too).
It's working if I input characters (both events are fired once) It's not working if I try to delete characters in any form (backspace, delete, etc) It won't delete any character and the cursor always gets reset to position 0. I used box.SelectionStart to move the cursor and it's working when I input characters.
I put a counter kinda thing to check and I found that when I tried to delete characters, both events are fired twice.
I tried to search but I got mixed answers...
Thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;`enter code here`
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Threading;
namespace SharedFileEditor
{
public partial class EditorView : Window
{
private EditorModel model;
private FileSystemWatcher watcher;
private string path;
private int count = 0;
private int count2 = 0;
public EditorView()
{
InitializeComponent();
model = new EditorModel();
this.DataContext = model;
}
private void OpenClicked(object sender, RoutedEventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "Text files (*.txt)|*.txt";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
watcher = new FileSystemWatcher(System.IO.Path.GetDirectoryName(dialog.FileName), "*.txt");
Console.WriteLine(System.IO.Path.GetDirectoryName(dialog.FileName));
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
path = dialog.FileName;
HandleOpen(dialog.FileName);
}
}
}
internal void HandleOpen(string path)
{
FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader reader = new StreamReader(f);
model.Content = reader.ReadToEnd();
reader.Close();
}
private void OnChanged(object source, FileSystemEventArgs e)
{
if (this.Box.Dispatcher.CheckAccess())
{
try
{
FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader reader = new StreamReader(f);
model.Content = reader.ReadToEnd();
this.Box.CaretIndex = model.Cursor;
reader.Close();
Console.WriteLine("read:" + count2++);
}
catch (IOException x)
{
Console.WriteLine(x.Message);
}
}
else
{
this.Box.Dispatcher.Invoke(
new updateContent(OnChanged), source, e);
}
}
private void ContentChanged(object sender, TextChangedEventArgs e)
{
FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(f);
writer.Write(this.Box.Text);
model.Cursor = this.Box.SelectionStart;
model.Content = this.Box.Text;
writer.Close();
Console.WriteLine("write:"+count++);
}
public delegate void updateContent(object source, FileSystemEventArgs e);
}
}
I figured it out... it has something to do with how my application works. Solved by setting the EnableRaisingEvents flag to false and back to true later.