Is there a way to prevent Windows from caching network files?

Viewed 941

I have a piece of commercial software that is writing out customer information to a binary file on a networked Windows drive. The architecture prevents me from using anything other than this networked drive.

I have a C# application that I've written to parse data from this binary file. This application simply sits in a loop, waiting for that file to be modified, then it parses the customer information looking for what changed. It will then update the customer's record in my own custom database.

The problem:

  • I use the commercial package to update a customer record (I can even hex dump the file and see the modification)
  • My C# application reads this same file (presumably), but it's not seeing the customer updates. What it is reading is identical to the previous parse

I'm a Linux guy and my first thought was that I've seen similar issues on Linux NFS mounts, and fixed them although it's been so long ago I don't remember the exact solution.

I've tried:

  • Using the group policy editor to disable offline files for all files. This didn't work.

  • Generic long sleeps before reading the updated binary files after a customer has been changed. This didn't work.
    ** Interesting note here is that my code will detect the modified time stamp and process it, but the contents seem to be cached still

  • I've triple checked all my internal data structures to ensure they're clean on each iteration. No buffered data there.

  • Even found one hack online where a guy said if you add a file to that same directory, then delete it, it somehow forces a directory refresh. This did not work but highlighted the problem in a different way. If I create the temp/hack file then immediately try to delete it, it doesn't work because my application doesn't see the new hack file. Even with a couple second sleep thrown between the creation and deletion.

  • I've tried, programatically, to move this file to the same file name with an _ on the end before reading it. My thought was that writing this new file, which would always be unique, would somehow force Windows to give me the latest and greatest from that folder. When I try to read this new/unique file, though, it tells me the file isn't found even though I can see it through Windows Explorer. Seems like I'm reading in some cached version of that directory.

Odd test behavior:

  • If I stop my application and restart it, it always reads the newest copy of the file. That's what led me first to rechecking all my internal data structures to see if I wasn't clearing a buffer or something. But that's not the case.

Operationally, there are multiple users and sites that are modifying this customer data so it's a lot of data changing very rapidly. I simply need a way to force a read of the actual data file. Thanks for any help any of you can provide.

UPDATE: Using some of the comments, I got it to work, but I wouldn't say I actually found a solution. It's just a hack. I just combined everything I thought should work before (but didn't) and gave it a test. First, I copy the file to the same directory with seconds since epoch tacked onto the end. Then, I go into a loop looking for that file by checking FileInfo.Exists. When it doesn't see it, I FileInfo.Refresh(), sleep a second, and try again. It's taken up to 15 seconds to break out of this loop in my bit of testing. After a minute of this, it'll error out just to prevent an endless loop here. Then I delete the temporary file.

Again, I know this is incredibly ugly and kludgy so I'm still interested in anyone with answers on how to handle this more properly.

0 Answers
Related