How do we get the complete file path using FileSystemWatcher ?

ipjyo
ipjyo used Ask the Experts™
on
Hi,
I am trying to use FileSystemWatcher to find if a file is present or not. Based on this it will raise an event to do some processing.
my code is as shown in the attached.
my question is, how do I get the complete file path including the file name?
currently when I try to see the value of
fsWatcher.Path.ToString()
it is holding only the directory path. In my example, it has only upto C:\Sample Files.
But I need to have the complete file path so it can pass the filename to another method where the actual processing will be done.
Thank you for any help!
Private Sub IntializeFileSystemWatcher()
            'Create File System Watcher for DAT files
            fsWatcher = New System.IO.FileSystemWatcher("C:\Sample Files", "*.DAT")
            ' Add event handlers for new files and change of existing files.
            'fsWatcher.Changed += new FileSystemEventHandler(OnFileChanged)
            AddHandler fsWatcher.Created, AddressOf OnFileCreated
            ' Begin watching.
            fsWatcher.EnableRaisingEvents = True
        End Sub
        Private Sub OnFileCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
            Dim ConvertXml As Translator
            ConvertXml = New Translator()
            Dim XmlDoc As New XmlDocument
            Try
                XmlDoc = ConvertXml.Prepare(fsWatcher.Path.ToString())
                XmlDoc.Save("c:\\dynamicsample.xml")
                '  MessageBox.Show("XML successful")
            Catch ex As Exception
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString(), "", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification)
            End Try
        End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Once you get the directory you have to loop through each files and get there names.

Here is a sample file system watcher which does what you want

http://www.vb-helper.com/howto_net_filesystemwatcher.html
In my projects I use System.IO.File.Exists(filename) to check if a file exists. The whole IO.File structure specifically serves the purpose of handling files. You can check out also IO.File.FileInfo class, which serves to store all sorts of information on files
The FileSystemEventArgs in the event handler specifies what file was affected.
In the OnFileCreated eventhandler, use:

e.FullPath

to get the filename.

Author

Commented:
Thank you all for the suggestions.
e.fullpath worked for me.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial