Why when using filcesystemwatcher and dictionary with fileinfo it's throwing exceptions on wrong key and file not found ?

Chocolade 536 Reputation points
2022-06-02T21:09:09.583+00:00

Link for the repository on github :

https://github.com/chocolade1972/FileSystemWatcher

The project size is 92 KB only compressed with winrar.

At the top i init with new instance both filelist and dic variables.
Then when starting watching i get all the files . and add them to the filelist and to the dic.

If for example the path is C:\ then filelist will contain 6 items :

filelist = Directory.GetFiles(textBox_path.Text, "*.*").ToList();  
                    foreach (var item in filelist)  
                    {  
                        FileInfo info = new FileInfo(item);  
                        dic.Add(item, info.Length);  
                    }  

207995-dic1.jpg

Then in the event Watcher_Changes at this line :

var info = new FileInfo(e.FullPath);  

There is exception the file not found and i see when using a breakpoint a long file temp type with tmp extension.
Then on other files it's throwing exceptions key was not present and then if i will start watching over again it will throw exceptions that the key is the same and already exist.

207897-dic2.jpg

Other problem is now the button Button_doit in the event Button_doit_Click is not changing to stop and stay on start all the time.

using System;  
using System.Collections.Generic;  
using System.Diagnostics;  
using System.IO;  
using System.Linq;  
using System.Security.Cryptography;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows;  
using System.Windows.Controls;  
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 Microsoft.Win32;  
  
namespace Watcher_WPF  
{  
    /// <summary>  
    /// MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        private Watcher watcher;  
        private List<string> filelist = new List<string>();  
        private Dictionary<string, long> dic = new Dictionary<string, long>();  
  
        public MainWindow()  
        {  
            InitializeComponent();  
  
            CenterWindowOnScreen();  
  
            var nf = new NFilters()  
            {  
                Size = true,  
                FileName = true,  
                DirectoryName = true,  
            };  
  
            watcher = new Watcher(nf);  
            watcher.Created += Watcher_Changes;  
            watcher.Changed += Watcher_Changes;  
            watcher.Deleted += Watcher_Changes;  
            watcher.Renamed += Watcher_Renamed;  
  
            PrintMsg(Application.ResourceAssembly.ToString());  
        }  
  
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)  
        {  
            if (watcher.IsStarted)  
            {  
                e.Cancel = MessageBox.Show("Watcher is working, are you sure to close?", "Oh!",  
                    MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No;  
            }  
        }  
  
        private void Window_KeyDown(object sender, KeyEventArgs e)  
        {  
            //Ctrl  
            if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl))  
            {  
                if (e.KeyboardDevice.IsKeyDown(Key.S))  
                {  
                    //Ctrl+S  
                    SaveLogs(null, null);  
                }  
                else if (e.KeyboardDevice.IsKeyDown(Key.D))  
                {  
                    //Ctrl+D  
                    ClearLogs(null, null);  
                }  
            }  
        }  
  
        private void Button_doit_Click(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                if (!watcher.IsStarted)  
                {  
                    SetPath(textBox_path.Text.Trim());  
                }  
  
                watcher.IsStarted = !watcher.IsStarted;  
  
                if(button_doit.Content.ToString() == "Stop")  
                {  
                    add_path.IsEnabled = true;  
                }  
                else  
                {  
                    add_path.IsEnabled = false;  
  
                    RandomFiles.GenerateFiles();  
  
                    filelist = Directory.GetFiles(textBox_path.Text, "*.*").ToList();  
                    foreach (var item in filelist)  
                    {  
                        FileInfo info = new FileInfo(item);  
                        dic.Add(item, info.Length);  
                    }  
                }  
  
                button_doit.Content    = watcher.IsStarted ? "Stop" : "Start";  
                button_doit.Foreground = watcher.IsStarted ? Brushes.Red : Brushes.Black;  
                textBox_path.IsEnabled = !watcher.IsStarted;  
  
                PrintMsg(watcher.IsStarted ? "watcher started...At " + DateTime.Now.ToString() : "watcher stopped...At " + DateTime.Now.ToString());  
            }  
            catch (Exception ex)  
            {  
                PrintErr(ex);  
            }  
        }  
  
        private void Button_options_Click(object sender, RoutedEventArgs e)  
        {  
            options.IsOpen = true;  
        }  
  
        private void OptionsMenu_Opened(object sender, RoutedEventArgs e)  
        {  
            allow_edit.IsChecked = !richTextBox_main.IsReadOnly;  
            topmost_switcher.IsChecked = Topmost;  
  
            filter_setter.IsEnabled  = !watcher.IsStarted;  
            include_subdir.IsEnabled = !watcher.IsStarted;  
            include_subdir.IsChecked = watcher.IncludeSubdirectories;  
        }  
  
        private void AddPath(object sender, RoutedEventArgs e)  
        {  
            // Configure open file dialog box  
            var dialog = new Microsoft.Win32.OpenFileDialog();  
            dialog.FileName = "Document"; // Default file name  
            dialog.DefaultExt = ".*"; // Default file extension  
            dialog.Filter = "All files|*.*"; // Filter files by extension  
  
            // Show open file dialog box  
            bool? result = dialog.ShowDialog();  
  
            // Process open file dialog box results  
            if (result == true)  
            {  
                // Open document  
                textBox_path.Text = dialog.FileName;  
            }  
        }  
  
        private void SaveLogs(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                SaveFileDialog sfd = new SaveFileDialog()  
                {  
                    Filter = "TXT|*.txt",  
                    FileName = $"watcher_{DateTime.Now:yyyyMMddHHmmss}"  
                };  
                if (sfd.ShowDialog() == true)  
                {  
                    string path = sfd.FileName;  
                    string content = new TextRange(  
                    richTextBox_main.Document.ContentStart,  
                    richTextBox_main.Document.ContentEnd).Text;  
  
                    File.WriteAllText(path, content);  
                    PrintMsg($"saved log to \"{path}\"");  
                }  
            }  
            catch (Exception ex)  
            {  
                PrintErr(ex);  
            }  
        }  
  
        private void ClearLogs(object sender, RoutedEventArgs e)  
        {  
            richTextBox_main.Document.Blocks.Clear();  
        }  
  
        private void Switch_Window_Topmost(object sender, RoutedEventArgs e)  
        {  
            Topmost = !Topmost;  
        }  
  
        private void Allow_edit_Click(object sender, RoutedEventArgs e)  
        {  
            richTextBox_main.IsReadOnly = !richTextBox_main.IsReadOnly;  
        }  
  
        private void Filter_setter_Click(object sender, RoutedEventArgs e)  
        {  
            var tmp = new FilterWindow(watcher.NFilters, watcher.Filter) { Owner = this }.ShowDialog();  
  
            if (tmp is Fdr fdr)  
            {  
                watcher.Filter = fdr.Filter;  
                watcher.NFilters = fdr.Nfilters;  
  
                PrintMsg($"set Filter: Size={watcher.NFilters.Size}, FileName={watcher.NFilters.FileName}, " +  
                    $"DirectoryName={watcher.NFilters.DirectoryName}, Filter=\"{watcher.Filter}\"");  
            }  
        }  
  
        private void Include_subdir_Click(object sender, RoutedEventArgs e)  
        {  
            watcher.IncludeSubdirectories = !watcher.IncludeSubdirectories;  
            PrintMsg($"IncludeSubdirectories={watcher.IncludeSubdirectories}");  
        }  
  
        private void View_source_code_Click(object sender, RoutedEventArgs e)  
        {  
            Process.Start("https://github.com/Mzying2001/Watcher_WPF");  
        }  
  
        private void TextBox_path_KeyDown(object sender, KeyEventArgs e)  
        {  
            if (e.Key == Key.Enter)  
                Button_doit_Click(null, null);  
        }  
  
        private void RichTextBox_main_TextChanged(object sender, TextChangedEventArgs e)  
        {  
            if (richTextBox_main.IsReadOnly)  
                richTextBox_main.ScrollToEnd();  
        }  
  
        private void SetPath(string path)  
        {  
            watcher.Path = path;  
            PrintMsg($"set path: \"{watcher.Path}\"");  
        }  
  
        private void Watcher_Changes(object sender, FileSystemEventArgs e)  
        {  
            try  
            {  
                if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Deleted)  
                {  
  
                }  
                else  
                {  
                    var info = new FileInfo(e.FullPath);  
                    var newSize = info.Length;  
  
                    var oldsize = dic[e.FullPath];  
  
                    string FileN1 = "File Name : ";  
                    string FileN2 = info.Name;  
                    string FileN3 = " Size Has Changed : From ";  
                    string FileN4 = oldsize.ToString();  
                    string FileN5 = " To ";  
                    string FileN6 = newSize.ToString();  
                    if (newSize != oldsize)  
                    {  
                        //string output = $"[*] {e.ChangeType}: \"{e.FullPath}\"";  
  
                        Println(FileN1 + FileN2 + FileN3 + FileN4 + FileN5 + FileN6);  
                    }  
  
                    if (dic.ContainsKey(e.FullPath))  
                    {  
                        dic[e.FullPath] = info.Length;  
                    }  
                    else  
                    {  
                        dic.Add(e.FullPath, info.Length);  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                PrintErr(ex);  
            }  
        }  
  
        private void Watcher_Renamed(object sender, RenamedEventArgs e)  
        {  
            try  
            {  
                Println($"[*] {e.ChangeType}: \"{e.OldFullPath}\" -> \"{e.FullPath}\"");  
            }  
            catch (Exception ex)  
            {  
                PrintErr(ex);  
            }  
        }  
  
        private void Println(string text, SolidColorBrush brush) => Dispatcher.Invoke(() =>  
        {  
            richTextBox_main.Document.Blocks.Add(new Paragraph(new Run(text) { Foreground = brush }));  
        });  
  
        private void Println(string text)  
        {  
            Println("[+] " + text + " At : " + DateTime.Now.ToString(), Brushes.LawnGreen);  
        }  
  
        private void PrintMsg(string message)  
        {  
            Println($"[+] {message}", Brushes.Yellow);  
        }  
  
        private void PrintErr(Exception e)  
        {  
            Println($"[-] {e}", Brushes.Red);  
        }  
  
        private void CenterWindowOnScreen()  
        {  
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;  
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;  
            double windowWidth = this.Width;  
            double windowHeight = this.Height;  
            this.Left = (screenWidth / 2) - (windowWidth / 2);  
            this.Top = (screenHeight / 2) - (windowHeight / 2);  
        }  
  
        private byte[] CheckSize(string filename)  
        {  
            using (MD5 md5 = MD5.Create())  
            {  
                using (var stream = File.OpenRead(filename))  
                {  
                    return md5.ComputeHash(stream);  
                }  
            }  
        }  
  
        private void textBox_path_TextChanged(object sender, TextChangedEventArgs e)  
        {  
            if (button_doit != null)  
            {  
                if (textBox_path.Text == "")  
                {  
                    button_doit.IsEnabled = false;  
                }  
                else  
                {  
                    button_doit.IsEnabled = true;  
                }  
            }  
        }  
    }  
}  
Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-06-03T07:28:10.94+00:00

    @Chocolade , Welcome Microsoft Q&A, based on my test, I reproduced your problem. I find that the reason occurs the exception about could not find the file is that the app does not have admin power.

    You could try the following steps to force your app to run as administrator.

    First, Please add new item->Application Manifest File.

    Second, Please change the <requestedExecutionLevel> element to the following:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
    

    Third, Please restart your visual studio and open the project.

    Finally, if you click start button, the previous exception will disappear.

    Hope my solution could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.