Maintaining an artifical file lock to prevent virus scanning software accessing file

32 views Asked by At

As the title states, i need a way for my C# application to artificially hold a file lock. In our application, we maintain a file lock of a System.IO.Package zip file (which i assume internally maintains a FileStream lock). However, at certain points we need to copy the file to create a backup. This can be done as rapidly as every few seconds. The processes is done as following

  1. Flush and Close the package file
  2. Copy the file to the backup location
  3. Open the package file again
  4. Continue writing to the package file

This is the way that this application has worked for 10 odd years, however, in recent times, with antivirus file scanning getting more and more aggressive, in the split second that we release the file lock to copy when we try to grab it again, we find that the file is locked and getting scanned and bad things start to happen.

Currently, we are just waiting and retrying to access the file every 500ms, but sometimes it can take over 10s to get the file back, or worse,the virus scanner releases the file lock, but somehow holds lock over the some files inside the zip. This causes all sorts of chaos for the application as it tries to write new data to these file.

This is software that we provide to external customers who are not always allowed to simply disable virus scanning so that is not an option unfortunately.

I need a way for the application to hold lock across all 4 steps to hopefully prevent virus scanning software from stealing the lock and causing mahem

0

There are 0 answers