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
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
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.
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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.
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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.:)
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
:)
(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.
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384