0

I'm setting a file a newName at runtime "rename " context menu strip item clicked and want to FileSystemWatcher.Renamed Event function properly

I'm trying to make File Explorer in c# window form

 private void renameToolStripMenuItem_Click(object sender, EventArgs e)
    {

        FileSystemWatcher watcher = new FileSystemWatcher(path_textBox.Text);


            //the renaming of files or directories.
            watcher.NotifyFilter = NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.FileName
                                 | NotifyFilters.DirectoryName;

            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.Error += new ErrorEventHandler(OnError);
            watcher.EnableRaisingEvents = true;

    }
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        //  Show that a file has been renamed.
        WatcherChangeTypes wct = e.ChangeType;
        MessageBox.Show($"File: {e.OldFullPath} renamed to {e.FullPath}");
    }

In renameToolStripMenuItem_Click event OnRenamed event is not running after calling

  • 1
    "In renameToolStripMenuItem_Click event OnRenamed event is not running after calling" that's because this method doesn't actually rename a file, it simply creates the FileSystemWatcher and telling him which method to invoke when he detects that a file has been renamed – MindSwipe Mar 27 '19 at 08:09
  • then how i rename file as file explorer(windows) is done – Arsalan sajjad touqeer Mar 27 '19 at 09:05
  • Renaming a file doesn't really rename a file. It moves it to the same location with a new name behind the scenes. [This](https://stackoverflow.com/a/3218923/9363973) Stack Overflow answer shows a easy and simple way to do this. By the way, are you doing this inside the Form class? If not inside which class? I'd like to know this so I can make a good example of how it should look – MindSwipe Mar 27 '19 at 09:18

1 Answers1

0

You're FileSystemWatcher (FSW) is configured correctly, but you're not renaming the file and thereby the FSW isn't raising the OnRename event. Here is a quickly thrown together example that should work:

class YourClass
{
    private FileSystemWatcher _watcher;

    // You want to only once initialize the FSW, hence we do it in the Constructor
    public YourClass()
    {    
         _watcher = new FileSystemWatcher(path_textBox.Text);

         //the renaming of files or directories.
         watcher.NotifyFilter = NotifyFilters.LastAccess
                             | NotifyFilters.LastWrite
                             | NotifyFilters.FileName
                             | NotifyFilters.DirectoryName;

         watcher.Renamed += new RenamedEventHandler(OnRenamed);
         watcher.Error += new ErrorEventHandler(OnError);
         watcher.EnableRaisingEvents = true;
    }

    private void renameToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // Replace 'selectedFile' and 'newFilename' with the variables
        // or values you want (probably from the GUI)
        System.IO.File.Move(selectedFile, newFilename);
    }

    private void OnRenamed(object sender, RenamedEventArgs e)
    {
        // Do whatever
        MessageBox.Show($"File: {e.OldFullPath} renamed to {e.FullPath}");
    }

    // Missing the implementation of the OnError event handler
}
MindSwipe
  • 7,193
  • 24
  • 47