Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:58+00:00 2026-06-17T08:27:58+00:00

I’ll try my best to explain my situation. I’m developing a software using C#,

  • 0

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);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:27:59+00:00Added an answer on June 17, 2026 at 8:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.