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

Submitted by: 
Visitors have accessed this post 177 times.

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:

output

3. Now, lets do the coding.

We will first code for our FileWatcher that will have the created as an event for this component.

  1. Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
  2.  
  3. If e.Name.Contains("\") = False And New FileInfo(e.FullPath).Extension = Nothing Then
  4.  
  5. Directory.Delete(e.FullPath)
  6. Else
  7.  
  8. File.Delete(e.FullPath)
  9. End If
  10.  
  11. End Sub

For locating or browsing files and folders, put this code in your browse button.

  1. Dim fbd As New FolderBrowserDialog
  2.  
  3. Private Sub ButtonBrowse2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse2.Click
  4. If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  5. TextBox1.Text = fbd.SelectedPath
  6. End If
  7. 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.

  1. Private Sub ButtonAllow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAllow.Click
  2. FileSystemWatcher1.EnableRaisingEvents = False
  3.  
  4. End Sub

And lastly, for disallowing files and folders to be created put this code in your disallowed button.

  1. Private Sub Buttondontallow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttondontallow.Click
  2.  
  3. If Not TextBox1.Text = Nothing Then
  4. FileSystemWatcher1.Path = TextBox1.Text
  5. FileSystemWatcher1.EnableRaisingEvents = True
  6. FileSystemWatcher1.IncludeSubdirectories = True
  7. Else
  8. MessageBox.Show("Please enter the path, you wish to monitor")
  9. End If
  10. 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.