How to detect files

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
I have an app that I want to sit and wait for two files that are being created from another application. When these files are detected I want the screen to be updated and a button enabled to allow further processing of these two require files.

This is what Im attempting but with little success:

Code:
Do While True

  Application.DoEvents()

   if file one exists update screen

   if file two exist update screen

   if both files exits enable button and jump out of loop

Loop

Is this something that I should be using a Thread on? Bear in mind I havent used threads before which is why for something so simple Im using the DoEvents method:)
 
you could always try using the io.systemfilewatcher , eg:
Code:
   Dim WithEvents fw As IO.FileSystemWatcher

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        fw = New IO.FileSystemWatcher("C:\testing")
        With fw
            .EnableRaisingEvents = True
            .WaitForChanged(IO.WatcherChangeTypes.Created)
            .BeginInit()
            Application.DoEvents()
        End With
    End Sub

    Private Sub fw_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fw.Changed
        If e.Name = "first file created" Then
            //do stuff when the file gets made.
        ElseIf e.Name = "second file created" Then
            //do stuff for the other file.
        End If
    End Sub
hope it puts you on track :)
 
Use the FileSystemWatcher component, its in your toolbox under Components section or IO.FileSystemWatcher. Set the filter to monitor a directory and use the Created event to check the file names.
 
Okey dokey thanks chaps, Ill give it a whirl.

I did get my code to work eventually like this.......I take it is is less efficient than the filewatcher?

Code:
            Do While True

                 let the computer carry on with background events

                Application.DoEvents()

                 check for files and if they exist update the screen

                fFile1 = New FileInfo(strFileP1)

                If fFile1.Exists Then

                    Me.chkP1Detected.Checked = True

                End If

                fFile2 = New FileInfo(strFileP2)

                If fFile2.Exists Then

                    Me.chkP2Detected.Checked = True

                End If

                 if both files exist enable button to allow user to generate p3.txt interface file
                 and jump out of loop

                If Me.chkP1Detected.Checked And Me.chkP2Detected.Checked Then

                    Me.cmdGenerateP3.Enabled = True

                    Exit Do

                End If
            Loop

ps congratulations on the twins:):)
 
The FileSystemWatcher is much more efficient and proper to use.
It creates a system-wide hook that receives messages whenever
file changes take place. It is only "running" when its
needed, but your method is hogging (no pun intended) all kinds
of system resources.
 
man Is jus gotta say I lus your avitar....I cant stop laughin lol:)

Yep I recoded to use the filewatcher so no more hoggin :)
 
Back
Top