Todd Allen
Todd Allen

Reputation: 648

vb.net file watch returns an incorrect filename

I am attempting to use the VB.Net FileSystemWatcher class to watch a folder for the creation of Excel files. I do get a response when a .xls file is created in the folder, but am having a problem with the below code:

Private Sub AddWatch()

Dim watch As New FileSystemWatcher

        AddHandler watch.Changed, AddressOf FileChange
        watch.Filter = "*.*"
        watch.NotifyFilter = NotifyFilters.LastWrite
        watch.Path = "C:\Documents and Settings\my.user\Desktop\testing"
        watch.EnableRaisingEvents = True
End Sub

    Private Sub FileChange(ByVal obj As Object, ByVal e As System.IO.FileSystemEventArgs)

        Debug.Print("changetype is: " & e.ChangeType.ToString & ", path is: " & e.FullPath.ToString)
    End Sub

When I create a text file in this folder, I get back the proper filename("C:\Documents and Settings\my.user\Desktop\testing\foo.txt"). However, when I save an Excel file into the folder, the path is still correct, but the filename is garbage (differs each time even with the same filename, always 8 characters like "C:\Documents and Settings\my.user\Desktop\testing\DE0B5800".) Can't find a thing on this searching Google or here, and MSDN as usual is little help. Anyone run across this before or know where I can find more information?

Upvotes: 1

Views: 574

Answers (1)

Jay
Jay

Reputation: 57899

http://support.microsoft.com/kb/814068

In summary:

When Excel saves a file, Excel follow these steps:

  1. Excel creates a randomly named temporary file (for example, Cedd4100 with no file name extension) in the destination folder that you specified in the Save As dialog box. The whole workbook is written to the temporary file.
  2. If changes are being saved to an existing file, Excel deletes the original file.
  3. Excel renames the temporary file. Excel gives the temporary file the file name that you specified (such as Book1.xls) in the Save As dialog box.

Upvotes: 1

Related Questions