FileSystemWatcher

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
DeveloperJacob
VIP - Donator
VIP - Donator
Posts: 87
Joined: Sun May 27, 2012 12:40 pm

FileSystemWatcher
DeveloperJacob
Hello, I'm DeveloperJacob and I want to show you one cool thing you can do with the FileSystemWatcher. It is a program that watches for events of files. So if a file is deleted, the program says that a File is delete. But we have more events than only deleted, we have: Created, Renamed, Changed and Deleted.

What you need:

2 Labels
1 Listview
2 Buttons
1 Textbox (Set to Read Only)

For the text for the buttons, labels take look at the picture: (Don't create the listview items, we are going to make them programmaticly)

Image


For adding items to the listview I wrote this code: (Event: "Form_Load")
Code: Select all
        ListView1.Columns.Add("Type change", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Directory", 250, HorizontalAlignment.Left)
        ListView1.Columns.Add("Date / Time", 75, HorizontalAlignment.Left)
The next code is for the button with the text: "Browse" or "..." :
Code: Select all
        Dim fbd As New FolderBrowserDialog
        fbd.ShowDialog()
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = fbd.SelectedPath
            FileSystemWatcher1.Path = TextBox1.Text
        End If
For the main part of the program I wrote this code:
Code: Select all
Dim str(2) As String
        Dim itm As ListViewItem
        str(0) = "File Changed"
        str(1) = e.FullPath
        str(2) = My.Computer.Clock.LocalTime
        itm = New ListViewItem(str)
        itm.Name = "itmq"
        itm.ForeColor = Color.DarkGoldenrod
        ListView1.Items.Add(itm)
You must add this code to the Created, Renamed, Deleted and Renamed event of the FileSystemWatcher. Also you must change the text of "str(0)". On the code above the text is: "File Changed" but if you are programming the Deleted, you must change that to "File Deleted" and every event has it own color:

I have:

Deleted: Tomato
Created: Green
Changed: DarkGoldenrod
Renamed: Orange

But you may change it.

Another small but very important code:
Code: Select all
FileSystemWatcher1.Path = TextBox1.Text
The code is for setting the Path of the FileSystemWatcher.

The last code is for the checkbox: (Event: CheckedChanged)
Code: Select all
        If TextBox1.Text = "" Then
            CheckBox1.CheckState = CheckState.Checked
            MsgBox("You must select a path first.")
        Else
            If CheckBox1.Checked = True Then
                FileSystemWatcher1.IncludeSubdirectories = True
            ElseIf CheckBox1.Checked = False Then
                FileSystemWatcher1.IncludeSubdirectories = False
            End If
        End If

This is the end of the tutorial.

Download the Project Files below:
You do not have the required permissions to view the files attached to this post.
Last edited by DeveloperJacob on Sat Jul 14, 2012 3:41 pm, edited 1 time in total.
Image
User avatar
pip
VIP - Donator
VIP - Donator
Posts: 156
Joined: Tue Jul 12, 2011 3:13 am

Re: FileSystemWatcher
pip
Hello nice post and all but i have seen so many of these on youtube and throughout google 5 credits is a bit much for one of these other than that nice first post.
<a href="http://www.points2shop.com/s/xbox_point ... 5082"><img src="http://points2shop.com/images/promotion ... ricoxg.gif" border="0"/></a>
User avatar
DeveloperJacob
VIP - Donator
VIP - Donator
Posts: 87
Joined: Sun May 27, 2012 12:40 pm

Re: FileSystemWatcher
DeveloperJacob
pip wrote:
Hello nice post and all but i have seen so many of these on youtube and throughout google 5 credits is a bit much for one of these other than that nice first post.
Thanks for your reply! I will change the price.
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: FileSystemWatcher
comathi
This is a very nice tutorial. It's your first one, isn't it? Welcome to the site, if we haven't already welcomed you lol, and +rep for this nice tut ;)
User avatar
DeveloperJacob
VIP - Donator
VIP - Donator
Posts: 87
Joined: Sun May 27, 2012 12:40 pm

Re: FileSystemWatcher
DeveloperJacob
comathi wrote:
This is a very nice tutorial. It's your first one, isn't it? Welcome to the site, if we haven't already welcomed you lol, and +rep for this nice tut ;)
Yes it's my first tutorial and Thankyou for your reply! :D
Image
5 posts Page 1 of 1
Return to “Tutorials”