Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using FileSystemWatcher in VS 2005

Posted on 2009-04-08
13
Medium Priority
?
337 Views
Last Modified: 2013-11-26
I am trying to use the code below, but the OnCreated sub never gets hit.  I can do this in Form_Load correct?
Private watcher As New IO.FileSystemWatcher
 
    Private Sub emailForm_Load(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles MyBase.Load
 
 
        watcher.Filter = "*.xls"
        watcher.Path = AppDomain.CurrentDomain.BaseDirectory
        watcher.EnableRaisingEvents = True
        AddHandler watcher.Created, AddressOf OnCreated
 
    End Sub 'emailForm_Load
 
    Private Sub OnCreated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
 
end sub

Open in new window

0
Comment
Question by:rgn2121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24094929
what folder are you creating the files in? Your code expects the new files to be in the same folder as the exe you are launching (I think).

You may need to set the watcher.Path property to the folder in which you want new files created
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24094957
The new files are created in my base directory, which is where I am watching...or so I thought.  After it runs through I can check and see my files right where I thought they would be, but it is like the watcher isn't running.
0
 
LVL 11

Assisted Solution

by:bmatumbura
bmatumbura earned 2000 total points
ID: 24094964
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 11

Expert Comment

by:bmatumbura
ID: 24094973
Oh I see;

Try and monitor files in a specific folder e.g.:

watcher.Path = "C:\Temp"
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095021
am I using C:\Temp as a test...because in production I will have to monitor for files in Appdomain.CurrentDomain.BaseDirectory
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095043
I bet I know why...I have made it start on form load, but once it sees the files maybe I need to refresh the form becasue I am updating labels...
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095052
I tried me.refresh...that didn't work.  The OnCreated event does not fire....
0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24095054
You maybe right: Is the OnCreated event handler updating the labels on the form?
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095074
Well, in the OnCreated I look through the files in the folder and grab the names of anything ending in .XLS.  I just use the watcher to know the files have been created.  The OnCreated never fires though.  The files are excel files that are generated through code...I don't guess that matters.
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095111
Ok well, it is watching, because after my app created the 2 files that I am trying to watch for, I browsed to the folder, right clicked and tried to create a new excel worksheet and as soon as I did it hit my breakpoint at OnCreated.
For some reason it doesn't see when the file is created by the app...
0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24095146
strange though... it should fire
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24095155
I have added a handler for the changed event and it doesn't fire either.  The OnCreated hits every time that I right click and create a new xls sheet.  
I thought maybe it was because the COM object wasn't closed yet, so I went into task manager and closed anything with Excel and still nothing.  I am running out of ideas...
0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 0 total points
ID: 24095225
Okay...Got it!!!!   In code I load an Excel Template and fill it.  I just thought about what happens why you open a template...it creates a file as Templatename1.xls.  Well, because I open this template before I start watching for the the OnCreate, I never see it.  What I should have been watching for is the OnRenamed...
Once I changed my code to that which is below...it fired right away.
AddHandler watcher.Renamed, AddressOf OnRenamed
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question