This tutorial will teach you how to create a program that will use the File Watcher component allowing files or disallow creating files or folder in 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 FileSystemWatcher in the form. Insert one textbox named TextBox1 to display the filename, a button named ButtonBrowse2 for browsing the files, button named ButtonAllow, and a button named Buttondontallow. You must design your interface like this:
3. Now, lets do the coding.
We will first code for our FileWatcher that will have the created as an event for this component.
Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
If e.Name.Contains("\") = False And New FileInfo(e.FullPath).Extension = Nothing Then
Directory.Delete(e.FullPath)
Else
File.Delete(e.FullPath)
End If
End Sub
For locating or browsing files and folders, put this code in your browse button.
Dim fbd As New FolderBrowserDialog
Private Sub ButtonBrowse2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse2.Click
If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = fbd.SelectedPath
End If
End Sub
For allowing files or folders to be created, put this code for your allowed button. We will just use EnableRaisingEvents method of the FileWatcher to False.
Private Sub ButtonAllow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAllow.Click
FileSystemWatcher1.EnableRaisingEvents = False
End Sub
And lastly, for disallowing files and folders to be created put this code in your disallowed button.
Private Sub Buttondontallow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttondontallow.Click
If Not TextBox1.Text = Nothing Then
FileSystemWatcher1.Path = TextBox1.Text
FileSystemWatcher1.EnableRaisingEvents = True
FileSystemWatcher1.IncludeSubdirectories = True
Else
MessageBox.Show("Please enter the path, you wish to monitor")
End If
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