File Watcher - Allow or Disallow Renaming Files or Folder in VB.NET

This is a continuation of my other yutorial named File Watcher - Allow or Disallow Creating Files or Folder in VB.NET, but in this tutorial it will grant to rename or disallow renaming of files and folder using vb.net. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add one button named ButtonBrowse for browsing the folder path, a button named Button1 that will allow renaming of files and folder, and a button named Button2 that will disallow renaming of files and folder. Insert a FileSystemWatcher1FileSystemWatcher as our main component in this tutorial and one textbox for displaying the file path of the folder. You must design your interface like this: output 3. Now, we will do the coding. First we will locate the folder that we will allow or disallow the files that contains it. We will use FolderBrowserDialog in the browse button.
  1. Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
  2.  
  3. Dim fbd As New FolderBrowserDialog
  4.  
  5. If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  6. TextBoxPath.Text = fbd.SelectedPath
  7. End If
  8. End Sub
Then we will check if the file/folder path is a folder or a file using the rename method of our file watcher.
  1. Private Sub FileSystemWatcher1_Renamed(ByVal sender As System.Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
  2. 'check if its folder or file
  3. If New FileInfo(e.FullPath).Extension = Nothing Then
  4. My.Computer.FileSystem.RenameDirectory(e.FullPath, e.OldName)
  5. Else
  6. My.Computer.FileSystem.RenameFile(e.FullPath, e.OldName)
  7. End If
  8.  
  9.  
  10. End Sub
To allow renaming of files and folder, we will put the EnableRaisingEvents to False in the file watcher for Button1.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. FileSystemWatcher1.EnableRaisingEvents = False
  3. End Sub
Lastly, put this code below for disallowing of renaming the files or folder in the specific path in your Button2.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. FileSystemWatcher1.EnableRaisingEvents = True
  3. FileSystemWatcher1.Path = TextBoxPath.Text
  4. FileSystemWatcher1.IncludeSubdirectories = True
  5. End Sub
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment