Question Using DirectoryInfo() object with unexpected results from Windows NTFS

markf

New member
Joined
Aug 10, 2011
Messages
1
Programming Experience
5-10
I am new to this forum so apologies if I have placed this in the wrong topic. I have a VB.Net application on FW2.5 that runs for many days at a time. It polls a particular server and folder to see if there are files to process. I have had random problems that I finally traced today to a loop that checks the folder contents. It seems that when I retrieve the list of files for that folder, sometimes a filename is returned that actually does not exist in the folder. It was once there but no longer is.

The program has a MainControl sub that loops through the file list returned by this code:

Dim FolderObj As DirectoryInfo = New DirectoryInfo("C:\MyFolderName")
Dim FolderContents As FileSystemInfo() = FolderObj.GetFiles()
Dim FileEntry as FileInfo
.
.
.

For Each FileEntry in FolderContents
If Not File.Exists(FileEntry.Name) Then Exit For
.
.
.
Next

The problem is that when a file is removed from the folder, the FolderContents array still has an entry for that file, even though I am creating a new DirectoryInfo() object each time before I enter the loop. The problem is reproducible, but takes some time. What I mean is that if I start the program and immediately remove a file from the folder, the program will receive an updated folder contents list from Windows. However, a day or two later and the program is given a folder content list with the missing file, and so it just exits the loop (Exit For) without doing anything.

Is there some sort of caching of folder contents that I have to account for? I could just restart the program every day, but I really want it to work as expected. Thanks for any advice.
 
I assume the rest of your loop processes the file that is there, so rather than Exit For when a file in your array doesn't exist, just skip that file and move onto the next element in the array.

Obviously you may have a requirement that dictates doing things the way you are, and of course you're not resolving the root of the problem, however sometimes the more simple solution wins the day.
 
Back
Top