SystemFileWatcher event not raised after File.WriteAllLines

NoobCube

Member
Joined
Mar 2, 2014
Messages
12
Programming Experience
Beginner
Hello,
My app write to file and I also set systemfilewatcher on that file to monitor changes.
filewatcher is used for case when there is more than one instance of this app is running and they all will write to the same file so all the other instances of this app will be updated.
when I use writeAllLines or streamWriter etc. no event is raised.
but when I manually write to this file and save, it works.
:apthy:
help please
 
I just tested and it worked fine for me. I created a new WPF project and added the following code to the default Window:
using System;
using System.IO;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private FileSystemWatcher fsw = new FileSystemWatcher();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fsw.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            fsw.Filter = "Test.txt";
            fsw.NotifyFilter = NotifyFilters.LastWrite;
            fsw.Changed += fsw_Changed;
            fsw.EnableRaisingEvents = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.txt");

            File.WriteAllText(filePath, "Goodbye cruel world");
        }

        void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            MessageBox.Show(e.FullPath);
        }
    }
}
I saw the expected message both when I edited the specified file directly and also when I clicked the Button.
 
Hello,
Thank you for the replay!
I did a little research and found out that this problem occurs when I try to write to file on lan drive (network location)
I have USB drive connected to my router and the file is stored there, and in my app I specify the IP address and the directory.
what happens is when program write to this file, everything works fine, text get written to the file, but filesystemwatcher event not raised.
(I also set full R/W permissions to the file)
But when I write to the file directly it works as it should and the event raised.
and when I change the location of the file to file on my PC everything works fine.
that is very strange because in MSDN they say that Filesystemwatcher works for network and local folders.
I can't find any additional info about this...

here is my test app code:
C#:
[COLOR=#000000]namespace [/COLOR][COLOR=#2B91AF]FileSystemWatcherTest2[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]partial[/COLOR][COLOR=#00008B]class[/COLOR][COLOR=#2B91AF]Form1[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Form[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#2B91AF]int[/COLOR][COLOR=#000000] appId[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#2B91AF]FileSystemWatcher[/COLOR][COLOR=#000000] fs[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]

        [/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#2B91AF]Form1[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#2B91AF]InitializeComponent[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]

        [/COLOR][COLOR=#00008B]private[/COLOR][COLOR=#00008B]void[/COLOR][COLOR=#2B91AF]Form1_Load[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]object[/COLOR][COLOR=#000000] sender[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]EventArgs[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#2B91AF]Random[/COLOR][COLOR=#000000] rnd [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]Random[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]DateTime[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Now[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Second[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            appId [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] rnd[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Next[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]9999[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]

            [/COLOR][COLOR=#00008B]string[/COLOR][COLOR=#000000] path [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]@"\\IP.Address.here\drive\test.txt"[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]

            fs [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]FileSystemWatcher[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]@"\\IP.Address.here\drive\"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"*.txt"[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]EnableRaisingEvents[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]IncludeSubdirectories[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]false[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]NotifyFilter[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2B91AF]NotifyFilters[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]LastWrite[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
             [/COLOR][COLOR=#808080]/*   NotifyFilters.Attributes | NotifyFilters.CreationTime
                | NotifyFilters.DirectoryName | NotifyFilters.FileName
                | NotifyFilters.LastAccess | NotifyFilters.LastWrite
                | NotifyFilters.Security | NotifyFilters.Size;*/[/COLOR][COLOR=#000000]

            [/COLOR][COLOR=#00008B]if[/COLOR][COLOR=#000000](![/COLOR][COLOR=#2B91AF]File[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Exists[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000] 
            [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#2B91AF]File[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Create[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]

                [/COLOR][COLOR=#808080]// set full R/W permissions for everyone[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#2B91AF]DirectoryInfo[/COLOR][COLOR=#000000] dInfo [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]DirectoryInfo[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#2B91AF]DirectorySecurity[/COLOR][COLOR=#000000] dSecurity [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] dInfo[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]GetAccessControl[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
                dSecurity[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]AddAccessRule[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]FileSystemAccessRule[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]SecurityIdentifier[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]WellKnownSidType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]WorldSid[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]null[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#2B91AF]FileSystemRights[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]FullControl[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]InheritanceFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]ObjectInherit[/COLOR][COLOR=#000000]|[/COLOR][COLOR=#2B91AF]InheritanceFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]ContainerInherit[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]PropagationFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]NoPropagateInherit[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]AccessControlType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Allow[/COLOR][COLOR=#000000]));[/COLOR][COLOR=#000000]
                dInfo[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]SetAccessControl[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]dSecurity[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]

            [/COLOR][COLOR=#808080]// write text to file to raise fs_changed event[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000] sw [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]DateTime[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Now[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | App Id: "[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#000000] appId [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | File changed..."[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Close[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Dispose[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
            GC[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Collect[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#808080]// just in case... [/COLOR][COLOR=#000000]

            fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Changed[/COLOR][COLOR=#000000]+=[/COLOR][COLOR=#000000] fs_Changed[/COLOR][COLOR=#000000];[/COLOR][COLOR=#808080]// raise event only for the next instance of this app[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]

        [/COLOR][COLOR=#00008B]private[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#00008B]void[/COLOR][COLOR=#000000] fs_Changed[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]object[/COLOR][COLOR=#000000] sender[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]FileSystemEventArgs[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#2B91AF]MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]appId [/COLOR][COLOR=#000000]+ [/COLOR][COLOR=#800000]" | file changed!"[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR]
 
Handle FileSystemWatcher.Error event and see if there is more information there.
 
I just tried it, error event also not raised...:miserable:

Idk what to do, I can't proceed with my app update because its depends on this stuff
I though about another ways but using FileSystemWatcher is the most efficient way
is there any other way to do that? but without timers?
 
C#:
[COLOR=#000000]
            [/COLOR][COLOR=#808080]// write text to file to raise fs_changed event[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000] sw [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]DateTime[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Now[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | App Id: "[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#000000] appId [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | File changed..."[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Close[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
            sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Dispose[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
            GC[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Collect[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#808080]// just in case... [/COLOR][COLOR=#000000]

            fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Changed[/COLOR][COLOR=#000000]+=[/COLOR][COLOR=#000000] fs_Changed[/COLOR][COLOR=#000000];[/COLOR][COLOR=#808080]// raise event only for the next instance of this app[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]    [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR]
You add the changed event handler after you have written to the file. That is too late ;) Move it (up) to where you initialise the filesystemwatcher.
 
I did it because I start multiple instances of this app and I want the event to raise only when another instance of this app starts...
btw I think the event fire anyway even if its after the code.

about my question:
I found forum post at MS or somewhere where they say to decrease/increase the buffer size of the filesystemwatcher, I tried to set it to 65000, 51200, 100 etc and it still dosen't work...
 
I think the event fire anyway even if its after the code.
Sure, the event will be raised regardless but if you haven't registered your method to handle the event then your method won't be invoked when the event is raised.
 

Latest posts

Back
Top Bottom