i face a problem in checking file condition.
I wish to create a program that can check the condition file continuously while the program is running.

For code below that I wrote:

Private Sub StartTracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FILE_NAME As String = "\Program Files\Fast_Automatic_Accident_Notification_Through_SMS\Resources\contact4.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then

            MessageBox.Show("File already Exist")

                    ElseIf MessageBox.Show("File not Exist") Then

        End If

End Sub

It just can check the file condition only once, that this is only the program first load.

If I suddenly remove the file, it can’t show a message “ File not Exist” or if I suddenly receive a file and place at the folder, it can’t show me a message “File already Exist”

Kindly advice, How can I modify my code to continuously checking the file condition while the program is running.

thanks a lot.

You can use Timer control.
Set Enabled=True on Timer properties
Set Interval as you needed. Interval in milliseconds. (e.g 1000 = 1 Second)

Then put your checking code in Timer event :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ' Your Code        
End Sub

What is ur actual need? U want to keep checking for file exist or not all the time ur application is running?

You might want to check out the FileSystemWatcher control. I think it is better to let the system notify you of changes than to continuously poll the system. Wrox Press Visual Basic 2008 Programmer's Reference says:

The FileSystemWatcher class keeps an eye on part of the file system and raises events to let your program know if something changes. For example, you could make a FileSystemWatcher monitor a work directory. When a new file with a .job extension arrives, the watcher could raise an event and your application could process the file.

The FileSystemWatcher class’s constructor takes parameters that tell it which directory to watch and that give it a filter for selecting files to watch. For example, the filter might be “ *.txt ” to watch for changes to text files. The default filter is “ *.* ” , which catches changes to all files that have an extension.

Set the filter to the empty string “ ” to catch changes to all files including those without extensions.

The NotifyFilter property "Determines the types of changes that the watcher reports. This is a combination of values defined by the NotifyFilters enumeration and can include the values Attributes, CreationTime, DirectoryName, FileName, LastAccess, LastWrite, Security, and Size.

Edited 4 Years Ago by Reverend Jim: n/a

hi FileSystemWatcher is not support in window mobile 6 professional (compact framework), what can i solve this if i wish to use this method?

Actually the code i post before just the first step for my project. After that i wish to replace the message box to a button event.

the code should be:

Private Sub StartTracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim FILE_NAME As String = "\Program Files\Fast_Automatic_Accident_Notification_Through_SMS\Resources\contact4.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then

           Button2_Click(sender, New System.EventArgs())
 
        End If
End Sub

the system will operate like this:
1. i hope to run this code continually
2. once the mobile receive a file name "contact4.txt" and save at location
"\Program Files\Fast_Automatic_Accident_Notification_Through_SMS\Resources\",
the system will trigger the " Button2 ",
if the file not exist at the location, i will not do anything.

My question:
1. May i used timer to build this function?
2. FileSystemWatcher?
3. and my code is correct if paste in program load "StartTracking_Load"? or need to paste at other function?
3. or any other method can suggest for me?

See if this helps.

Public Class Form1
    Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" '// folder for file.
    Private myFile As String = myFolder & "contact4.txt" '// file to check if .Exists.
    Private WithEvents tmrFileExists As New Timer With {.Interval = 1000, .Enabled = True} '// dynamic Timer.

    Private Sub tmrFileExists_Tick(sender As Object, e As System.EventArgs) Handles tmrFileExists.Tick
        Button2.Enabled = IO.File.Exists(myFile) '// toggle Button2 Enabled/Disabled.
    End Sub
End Class

hi codeorder, thanks for the code.

but when i try the code:

Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" '// folder for file.

i get this error:

'Desktop' is not a member of 'System.Environment.SpecialFolder'.

i am using window mobile 6 professional. do i need add timer from toolbox for my project?

kindly advice. thanks a lot

Change myFolder to your folder.
Example: Private myFolder As String = "C:\Program Files\Fast_Automatic_Accident_Notification_Through_SMS\Resources\" '// folder for file. You do Not need to add a Timer, it has already been added dynamically on line.4.

hi codeorder, thanks for your reply. i appreciate.

Your code give me new idea in my project. i used your code for develop my project, but there is some problem.

bellow are my code

Public Class Form1

    Private myFolder As String = "\My Documents\"
    Private myFile As String = myFolder & "123.txt" '// file to check if .Exists.
    Private WithEvents tmrFileExists As New Timer With {.Interval = 1000, .Enabled = True} '// dynamic Timer.

    Private Sub tmrFileExists_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrFileExists.Tick

        If System.IO.File.Exists(myFile) = True Then
            Button1_Click(sender, New System.EventArgs())
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("file exits")
    End Sub
End Class

Actually is same with the coding you provide me. Bellow are your code

Button2.Enabled = IO.File.Exists(myFile) '// toggle Button2 Enabled/Disabled.

i just change to

If System.IO.File.Exists(myFile) = True Then
            Button1_Click(sender, New System.EventArgs())

the problem is:
i just wish to trigger the button only 1 time, for the code you provide me is trigger the button a lot of times and come to the end, the program end with error.

Can someone advise me how to trigger the button only one time?

thanks a lot

For this typeOf(issue), use a Boolean; reason: Booleans only toggle between a True/False and Nothing Else.

Public Class Form1
    Private myFolder As String = "\My Documents\"
    Private myFile As String = myFolder & "123.txt" '// file to check if .Exists.
    Private WithEvents tmrFileExists As New Timer With {.Interval = 1000, .Enabled = True} '// dynamic Timer.
    Private bIsButtonClickable As Boolean = True '// Boolean to determine if Button is clickable.

    Private Sub tmrFileExists_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrFileExists.Tick
        If mySelectedCoolFileExists() = True Then
            _Button1_Click(sender, New System.EventArgs())
        Else
            bIsButtonClickable = True
        End If
    End Sub

    Private Sub _Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If bIsButtonClickable = True Then
            If mySelectedCoolFileExists() = True Then
                bIsButtonClickable = False
                MessageBox.Show("file exits")
            End If
        End If
    End Sub
    Private Function mySelectedCoolFileExists() As Boolean
        If System.IO.File.Exists(myFile) = True Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

It checks twice for the File, though if it were not morning here, I would have had it check for the file only once, by using another Boolean of course.:)

Other than that, glad I could be your wing.man for the time being and glad I could help.:)

Edited 4 Years Ago by codeorder: CBool(comment)

Comments
solution

:)
(btw: that smiley isNot for reply to your post, though glad I could help; that smiley is to look down on the "Marked as Solved", for when it finally gets there. Guess I'm just one step ahead, as I seem to be.

my code is in console app and i run it using the command line so i won't be able to use buttons. please help

This article has been dead for over six months. Start a new discussion instead.