Watching Folder Activity in VB.NET
-
Never mind, figured it out!
-
I'm using the filewatcher and it seems to only raise events when file/dir changes occur on the local C drive. I'm using Win Xp Pro. I've tried to have it watch drives across the network using both mapped (Z:) and UNC (//servername/dir) and no events get raised.
The computer I'm trying to watch is a Windows 2000 O.S. Mine is xp pro.
Should this work or not?
Thanks
jds -
Hi. Truly loved the article ! Would like to know however, how I could also view the person who accessed or changed the file. If this application were to run on a Windows 2000 box that is part of a Windows 2000 domain, would it be possible to output the NT account that accessed the file ?
Also, I assume that the file change state would accomodate a file read so if somebody were to simply open a file to read and close without making any changes, shouldn't that raise the File Changed event ? Apparently this does not happen in my case.
Please advise.
Thanks -
I like the code and it works well as a regular windows app.
But I am trying to make it into a Windows service and I keep getting a System.IO.FileNotFoundException.
How do I alter the security so the system can see the directories?
The code fails on the following line:
WatchedFolder.Path = DistillerPath
I tried to see if the path exists first, but this always returns false
System.IO.Directory.Exists(DistillerPath)
Thanks. -
Imports System.IO
Imports System.Diagnostics
Imports System.ServiceProcess
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Public watchfolder As FileSystemWatcherRegion " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1
'
'Service1
'
Me.ServiceName = "Service1"
End SubEnd Region
Public Function StopWatch()
watchfolder.EnableRaisingEvents = False
WriteToFile("Service Stopped.")
End Function
Public Function StartWatch()
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
WriteToFile("Setting Folder to watch... ")
watchfolder = New System.IO.FileSystemWatcher("C:\mypath")
WriteToFile("""" & watchfolder.Path & """" & vbNewLine & "Done" & vbNewLine)
'this is the path we want to monitor
'watchfolder.Path = "C:\mypath"
' watchfolder.Path = txt_watchpath.Text
'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
'WriteToFile("01")
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
' add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
'End of code for btnstartclick
End Function
Public Function WriteToFile(ByVal prString As String)
FileOpen(1, "C:\temp\Log.log", OpenMode.Append)
Print(1, prString)
prString = ""
FileClose(1)
End Function
Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
WriteToFile("File" & e.OldName & " has been renamed to " & e.Name & vbCrLf)
'txtfolderactivity.Text &= "File" & e.OldName & _
'" has been renamed to " & e.Name & vbCrLf
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
WriteToFile("File " & e.FullPath & " has been modified" & vbCrLf)
'txtfolderactivity.Text &= "File " & e.FullPath & _
'" has been modified" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
WriteToFile("File " & e.FullPath & " has been created" & vbCrLf)
'txtfolderactivity.Text &= "File " & e.FullPath & _
'" has been created" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
WriteToFile("File " & e.FullPath & " has been deleted" & vbCrLf)
'txt_folderactivity.Text &= "File " & e.FullPath & _
'" has been deleted" & vbCrLf
End If
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code h -
i liked the folder watch very much... to use it more appropriately i just wanted to implement to unzip files once a folder receives any kind of zip files ...
i tried
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
txtfolderactivity.Text &= "File " & e.FullPath & _
" has been modified" & vbCrLf
ExecuteApplication("C:\WinRAR\WinRAR.exe x <source> <destn>
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
txtfolderactivity.Text &= "File " & e.FullPath & _
" has been created" & vbCrLf
ExecuteApplication("C:\WinRAR\WinRAR.exe x <source> <destn>
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
txt_folderactivity.Text &= "File " & e.FullPath & _
" has been deleted" & vbCrLf
ExecuteApplication("C:\WinRAR\WinRAR.exe x <source> <destn>
End If
End Sub
Public Sub ExecuteApplication(ByVal pstrApplicationName As String)
Dim udtProcess As Process = New Process
udtProcess.Start(pstrApplicationName)
udtProcess.Close()
End Sub
It doesnt work ... it opens the winrar and hangs up....
where do i go wrong in giving the command line syntax for unzipping ... and one more thing is it opens the winrar application which i think should work in the background like it does when i paste the command in START->RUN
thanks in anticipation Jayesh -
Hi,
This is wonderful article. I used it to write a small utility... kinda backup utility.
I have one problem though:
To test the application, I copied a big file (about 100 MB in size) from another folder on my computer, to the folder I was watching. It took few seconds to copy this 100 MB file to the watch folder.
However, my program tried to access this file immediately (while copying was not yet finished). So my program threw an exception saying the File is being used by another program.
How to make sure that the new file has been properly created in the watch folder before trying to access it?
(so in my case, I want my program to access this file only after it is completely copied to the watch folder)
Thanks in advance,
Vikram
-
15 years agoby jmurdock
Sandy Murdock
Peterborough Ontario, United StatesJoined 15 years agoI am working on an application to use the filewatcher (this is a windows service) to recognize when an xml file is placed in a directory, it is then to read the file and use it.
When ever I try to read the file I get the error that the file is in use. Since it isn't, I have to assume that the file watcher has somehow locked the file and I cannot open it.
How do I make the file watcher 'let go'??? -
What are you doing with your service? I need the same type of thing but I need to parse the xml and sent it to SQL server. I have done this in vbscript not a problem but I cannot seem to get it to work in VB.net I get cross thread errors. [email protected]
-
15 years agoby jmurdock
Sandy Murdock
Peterborough Ontario, United StatesJoined 15 years agoQuote: [1]Posted by jwillis27640 on 12 Jul 2005 05:14 PM[/1]
What are you doing with your service? I need the same type of thing but I need to parse the xml and sent it to SQL server. I have done this in vbscript not a problem but I cannot seem to get it to work in VB.net I get cross thread errors. [email protected]
I am doing exactly what you are doing. I resolved my file locked issue by doing this:
<code>
Do While IO.File.GetAttributes(strFileName) = FileAttributes.Offline
'Wait 1/2 second before trying again.
Threading.Thread.Sleep(500)
Loop
Threading.Thread.Sleep(500)
fnReadXML(strFileName)
</code>
fnReadXML works like this:
<code>
Function fnReadXML(ByVal strFileName As String)
Dim fStream As New FileStream(strFileName, FileMode.Open)
Dim reader As XmlTextReader = New XmlTextReader(fStream)
Dim dsPO As New DataSet
Dim cError As New CustomError
Dim cErrors As CustomError()
Try
dsPO.ReadXml(reader)
reader.Close()
fStream.Close()
'Data Set has 2 tables, form relationship.
fnMakeRelationship(dsPO)
'Now that there is a dataset, use it to do my insert into SQL. Return errors if there are any.
cErrors = fnInsertPOFromDS(dsPO, strFileName)
Catch ex As Exception
EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " XMLTextReader " & ex.ToString, EventLogEntryType.Error)
End Try
reader = Nothing
dsPO.Dispose()
File.Delete(strFileName)
</code> -
Thanks JMurDock!! Maybe you can help me with this. I am using Excel 2003 to parse the XML because I don't know what information might be in the file. It has varying field information so I do it like this in vbscript:
I am sorry I am kind of a newbie!! Is there a way to use a recordset in the same way and pass the information to addRecords as arrays?
My last question is really stupid but here goes... Is there a way not to have to use ODBC and use a provider instead?
<code>
'****************
Sub ParseXML(strFileName)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Open(strFileName)
strValue = ""
col = 1
row = (-2)
x = 1 'Excel
z = 1 'Excel
w = 2
y = 1
Do Until objExcel.Cells(x,1).Value = ""
row = row + 1
x = x + 1
Loop
Do Until objExcel.Cells(2,x).Value = ""
x = x + 1
col = x
Loop
ReDim rowHeader((col - 1))
x = 2
z = 0
for x = 2 to 2
for y = 1 to col
strValue = objExcel.Cells(x,y).Value
rowHeader(y-1) = RTRIM(strValue)
'Collect Row Header Data Here
next
next
x = x + 1
x = 3
for x = 3 to (row + 2)
ReDim rowArr((col - 1))
for y = 1 to col
rowArr(y-1) = objExcel.Cells(x,y).Value
'Collect Row Data Here
next
'y = y + 1
'Database Insert Here!
AddRecords rowHeader, rowArr
next
x = x + 1
objExcel.quit
End Sub
'****************
'****************
Sub AddRecords(strheader, strrowdata)
On Error Resume Next
i = 0
j = (UBound(strheader) - 1)
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "DSN=S795A54;UID=XXXX;pwd=XXXX"
objRecordset.CursorLocation = adUseClient
objRecordset.Open "FECS" , objConnection, _
adOpenStatic, adLockOptimistic
i = 0 ' reset counter to zero
objRecordset.AddNew
for i = 0 to j
objRecordset(strHeader(i)) = strrowdata(i) 'strRecords(i)
Next
'i = i + 1
objRecordset("DOCUMENT") = "\" & strComputer & "\dma\fecs\tiffs"
objRecordset("FUNCADDDATE") = now()
objRecordset.Update
objRecordset.Close
objConnection.Close
End Sub
'****************
</code> -
Hi I have tried this code to use it for watching a folder for tif files which I wish to print automatically. I have managed to get it to work in an application mode. However I cant seem to make it work as a service.
I have included eventlog entries in the found new file handlers however nothing gets logged to the eventlog.
below is the code: where have i gone wrong?
Imports System.io
Imports System.IO.FileSystemWatcher
Imports System.diagnostics
Imports System.Drawing.Printing
Imports System.ServiceProcess
Public Class folderWatch
Inherits System.ServiceProcess.ServiceBase
Public VARwatchfolder As FileSystemWatcher
Public mapImage As System.Drawing.Bitmap
Dim folderToWatch As String = "C:\folderWatchTest"
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New folderWatch, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New folderWatch}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1
'
'folderWatch
'
Me.ServiceName = "folderWatch"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
folderWatch()
System.Diagnostics.EventLog.WriteEntry("folderWatch", "FolderWatch service started!!! watching folder: " & folderToWatch)
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
VARwatchfolder.EnableRaisingEvents = False
End Sub
Public Function folderWatch()
'System.Diagnostics.EventLog.WriteEntry("folderWatch", "folderWatch Subroutine starts here")
'monitorTimer.Start()
VARwatchfolder = New System.IO.FileSystemWatcher
VARwatchfolder.Path = folderToWatch
VARwatchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
VARwatchfolder.NotifyFilter = VARwatchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
VARwatchfolder.NotifyFilter = VARwatchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
'VARwatchfolder.Filter = "*.tif"
AddHandler VARwatchfolder.Created, AddressOf logCreate
AddHandler VARwatchfolder.Changed, AddressOf logCreate
VARwatchfolder.EnableRaisingEvents = True
End Function
Private Sub logCreate(ByVal Source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
System.Diagnostics.EventLog.WriteEntry("folderWatch_change", "Found change in file: " & e.FullPath)
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
System.Diagnostics.EventLog.WriteEntry("folderWatch_new", "Found New file: " & e.FullPath)
Dim imageFile As New System.Drawing.Bitmap(e.FullPath)
mapImage = imageFile
Dim printer As String = PrinterSettings.InstalledPrinters.Item(0)
Dim pd As New PrintDocument
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
mapImage.Dispose()
System.Diagnostics.EventLog.WriteEntry("folderWatch_print", "Printed New file: " & e.FullPath)
Dim printedFilesPath As String
printedFilesPath = Replace(e.FullPath, folderToWatch, folderToWatch & "\printed")
' If CBmoveAfterPrint.Checked = True Then
System.IO.File.Move(e.FullPath, printedFilesPath)
System.Diagnostics.EventLog.WriteEntry("folderWatch", "will move file to " & printedFilesPath)
' & -
Although the file watcher provides notification of a file create, it provides this notification at the moment of the file "creation", not the moment of the file "completion" if you know what I mean. If I create a file, then wait two minutes and write data into the file, then wait another two minutes and close the file, file watch will fire the create event on the file create, not four minutes after, when the file is closed. Even worse, if you create the file but close it. The later open it and write data and close.
This is an old problem with watching for files. The question "How do I know that the file is finished creating?" has the same answer as "How do I know the cake is finished baking?". You can time it, check it, and when you think it's done try to eat it, if it's not done, put it back in the oven...
What I usually do is add the file path and file size to a list, and then wait a period of time. When the time has elapsed, I compare the current file size against the earlier file size. When the file size has not changed for two subsequent checks, I "Assume" the file is done.
This method is, of course, flawed, since a network outage, or a slow connection, or an Application that writes in stages, etc. etc. could break it. I wish that there was a more definitive way to know. The secret to making this work in all conditions is to be able to spit the file back out if it's not "done". (Try to eat the cake, and if it's not done, put it back in the oven and continue to wait.)
-
In VB2005 there is a painful cross-threading error that throws an InvalidOperationException when returning text from the fileSystemWatcher to the main form. This can be resolved by declaring a Delegate and adding a Sub routine to handle the cross-threading issue on the txtFolderActivity textbox.
Private Sub CheckCrossThread(ByVal message As String)
If Me.txtFolderActivity.InvokeRequired Then
Dim cct As New SetTextCallback(AddressOf CheckCrossThread) Me.Invoke(cct, New Object() {message})
Else Me.txtFolderActivity.Text = messageYou then just put the code in the logChange/Renamed sub int the CheckCrossThread routine.
-
hi, i tried the file watcher on text files in my document folder and i received this error any time i tried to a change to any text file.
InvalidOperationException was handled: Cross-thread operation not valid: Control 'txt_folderActivity' accessed from a thread other than the thread it was created on.
This is the code
Imports System.IO Imports System.Diagnostics Public Class Form1 Public watchfolder As FileSystemWatcher Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click watchfolder = New System.IO.FileSystemWatcher() 'this is the path we want to monitor watchfolder.Path = txt_watchpath.Text 'Add a list of Filter we want to specify 'make sure you use OR for each Filter as we need to 'all of those watchfolder.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.Attributes) ' Only watch text files. watchfolder.Filter = "*.txt" ' add the handler to each event AddHandler watchfolder.Changed, AddressOf logchange AddHandler watchfolder.Created, AddressOf logchange AddHandler watchfolder.Deleted, AddressOf logchange ' add the rename handler as the signature is different AddHandler watchfolder.Renamed, AddressOf logrename 'Set this property to true to start watching watchfolder.EnableRaisingEvents = True Me.btn_startwatch.Enabled = False Me.btn_stopwatch.Enabled = True 'End of code for btn_start_click End Sub Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Changed Then txt_folderActivity.Text &= "File " & e.FullPath & _ " has been modified" & vbCrLf End If If e.ChangeType = IO.WatcherChangeTypes.Created Then txt_folderActivity.Text &= "File " & e.FullPath & _ " has been created" & vbCrLf End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then txt_folderActivity.Text &= "File " & e.FullPath & _ " has been deleted" & vbCrLf End If End Sub Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) txt_folderActivity.Text &= "File" & e.OldName & _ " has been renamed to " & e.Name & vbCrLf End Sub Private Sub btn_stopwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stopwatch.Click ' Stop watching the folder watchfolder.EnableRaisingEvents = False btn_startwatch.Enabled = True btn_stopwatch.Enabled = False End Sub End Class
Please help me out
!--removed tag--> -
hei, just a day after i posted for help on this code, i've got the corrections. and this is it.
Imports System.IO Imports System.Diagnostics Public Class Form1 Public watchfolder As FileSystemWatcher Public str As String = "" Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click watchfolder = New System.IO.FileSystemWatcher() 'this is the path we want to monitor watchfolder.Path = txt_watchpath.Text 'Add a list of Filter we want to specify 'make sure you use OR for each Filter as we need to 'all of those watchfolder.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.Attributes) ' Only watch text files. watchfolder.Filter = "*.txt" ' add the handler to each event AddHandler watchfolder.Changed, AddressOf logchange AddHandler watchfolder.Created, AddressOf logchange AddHandler watchfolder.Deleted, AddressOf logchange ' add the rename handler as the signature is different AddHandler watchfolder.Renamed, AddressOf logrename 'Set this property to true to start watching watchfolder.EnableRaisingEvents = True Me.btn_startwatch.Enabled = False Me.btn_stopwatch.Enabled = True 'End of code for btn_start_click End Sub Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Changed Then If Me.txt_folderActivity.InvokeRequired Then str = "File " & e.FullPath & " has been modified" & vbCrLf txt_folderActivity.Invoke(New MethodInvoker(AddressOf AssignData)) Else txt_folderActivity.Text = str End If End If If e.ChangeType = IO.WatcherChangeTypes.Created Then If Me.txt_folderActivity.InvokeRequired Then str = "File " & e.FullPath & " has been created" & vbCrLf txt_folderActivity.Invoke(New MethodInvoker(AddressOf AssignData)) Else txt_folderActivity.Text = str End If End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then If Me.txt_folderActivity.InvokeRequired Then str = "File " & e.FullPath & " has been deleted" & vbCrLf txt_folderActivity.Invoke(New MethodInvoker(AddressOf AssignData)) Else txt_folderActivity.Text = str End If End If End Sub Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Renamed Then If Me.txt_folderActivity.InvokeRequired Then str = "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf txt_folderActivity.Invoke(New MethodInvoker(AddressOf AssignData)) Else txt_folderActivity.Text = str End If End If End Sub Private Sub btn_stopwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stopwatch.Click ' Stop watching the folder watchfolder.EnableRaisingEvents = False btn_startwatch.Enabled = True btn_stopwatch.Enabled = False End Sub Private Sub AssignData() Me.txt_folderActivity.Text = str End Sub End Class
thanks to rgkamble
!--removed tag--> -
I've also tested the code using a different additional codes from ibless
So, this is what i came up with. this code solves the renamed problem i posted some hours ago.
!--removed tag-->Imports System.IO Imports System.Diagnostics Imports System Imports System.ComponentModel Imports System.Threading Imports System.Windows.Forms Public Class Form1 Public watchfolder As FileSystemWatcher Public str As String = "" Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click watchfolder = New System.IO.FileSystemWatcher() 'this is the path we want to monitor watchfolder.Path = txt_watchpath.Text 'Add a list of Filter we want to specify 'make sure you use OR for each Filter as we need to 'all of those watchfolder.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.Attributes) ' Only watch text files. watchfolder.Filter = "*.txt" ' add the handler to each event AddHandler watchfolder.Changed, AddressOf logchange AddHandler watchfolder.Created, AddressOf logchange AddHandler watchfolder.Deleted, AddressOf logchange ' add the rename handler as the signature is different AddHandler watchfolder.Renamed, AddressOf logrename 'Set this property to true to start watching watchfolder.EnableRaisingEvents = True Me.btn_startwatch.Enabled = False Me.btn_stopwatch.Enabled = True End Sub Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Changed Then ' txt_folderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf Dim b As String b = "File " & e.FullPath & " has been modified" ' & vbCrLf SetText(b) End If If e.ChangeType = IO.WatcherChangeTypes.Created Then ' txt_folderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf Dim b As String b = "File " & e.FullPath & " has been created" ' & vbCrLf SetText(b) End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then ' txt_folderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf Dim b As String b = "File " & e.FullPath & " has been deleted" ' & vbCrLf SetText(b) End If End Sub Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Renamed Then ' txt_folderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf 'change it to this or retype it in this code : Dim b As String b = "File " & e.OldName & " has been renamed to " & e.Name & vbCrLf SetText(b) End If End Sub Private Sub btn_stopwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stopwatch.Click ' Stop watching the folder watchfolder.EnableRaisingEvents = False btn_startwatch.Enabled = True btn_stopwatch.Enabled = False End Sub Private Sub SetText(ByVal [text] As String) If Me.txt_folderactivity.InvokeRequired Then Dim d As New ContextCallback(AddressOf SetText) Me.Invoke(d, New Object() {[text]}) Else Me.txt_folderactivity.Text &= [text] & vbCrLf End If End Sub End Class
Post a reply
VB.NET forum discussion
-
ICSharp ZIP library C# to VB.NET Port
by Garry Wang (6 replies)
-
Printing with formatting in VB.NET
by jclebon (7 replies)
-
IP2Location Geolocation .NET Component in VB.NET
by dunking (5 replies)
-
Watching Folder Activity in VB.NET
by emmaddai (17 replies)
-
print a document file without using print dialog control in Vb.net
by konikula (1 replies)
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).