Sorry for the long post, but I want to give enough information to explain the problem I'm running into. I'm writing a windows service in VB.Net that has to monitor specific directories and when a file is found in that directory it is moved to a processing directory. I use two methods of monitoring the directory. First is a manual scan function that handles all the files using a for loop that looks like this:
I then use a similar loop and recursively call the manual scan function for each sub-directory. After that I set up a FileSystemWatcher to monitor the directory. I also have wrote code that tracks when the last notification for any file was received and jumps out if the previous notification for that file was received within the last few seconds so when multiple writes occur during the transfer of the file to the directory the application doesn’t try to process the same file multiple times. After that I have a loop that tries to open the file with write access to make sure that the file still isn’t in the middle of the file transfer before trying to move it to the processing directory. My code to do this looks like this:
This code has worked just fine for files stored on my local computer, even if I access it via a shared directory and UNC path. All I had to do was make sure the service was running with under the Local System account and make sure any directories that were used by the service were set up to give access to the Local System account. When I set up the application to monitor a remote server everything works fine up until this point. The service can see the file during the manual file scan, and gets notification through the FileSystemWatcher if I copy the file out to the directory while the service is running. For some reason that I can’t figure out the above code can’t gain write access to the file and the File.Open function throws the exception: "Access to the path "\\cmpfilep03\df...\testfile.txt" is denied." The directory and file both have the security privileges set up so the SYSTEM account has full access. I thought that the file might be in use by another process, but I can open the file in notepad, make a change, and save it without any problems. Does anybody have any ideas what could be causing this? Thanks.
VB.NET:
For Each sFile in Directory.GetFiles(sPath)
ProcessFile(sFile)
Next
VB.NET:
bFileOpened = False
For nCount = 1 To MAX_TRIES
Try
objFileStream = File.Open(sFilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
objFileStream.Close()
bFileOpened = True
Exit For
Catch
Thread.Sleep(WAIT_TIME)
End Try
Next