3

I want my program to be able to make a copy of a file before another program changes it's contents. I looked into FileSystemWatcher, but I don't think that will be able to help me, as I didn't see any thing about being able to catch or prevent events.

One solution I thought of was detecting when a program open a file with write privileges, and making a copy in memory at that point. I don't know how to detect when a file is opened by another program though.

Incase my desired results are unclear, here is an example. Audio files are linked to the music folders of multiple users on a system to save disk space. If a user want to modify the metadata of a music file, or apply some sort audio effect to a file, the modification shouldn't effect the other user's copies. The user should automatically get his own copy upon making a change.

drdrez
  • 921
  • 1
  • 7
  • 16
  • Here is a similar question that was answered before http://stackoverflow.com/questions/1352530/watch-for-files-in-a-folder-in-vb-net – Ahmad Nov 05 '12 at 08:08
  • 3
    You need a file system filter driver, but turning a write into more writes can be problematic. (You are breaking file system contracts when you pull tricks like this.) Better would be to use the volume snapshot service or the single instance storage sevice. – Raymond Chen Nov 05 '12 at 08:09
  • This is done with help of filesystem filter driver. Writing one is a tricky thing (as it's a kernel-mode driver), but we have a product, CallbackFilter, which provides a ready to use driver and lets you write business logic in user mode. – Eugene Mayevski 'Callback Nov 05 '12 at 10:31
  • @RaymondChen After researching some of the items you mentioned, I think I found something that will work! Thank you! I will answer my own question with the information I found. – drdrez Nov 05 '12 at 17:58

1 Answers1

2

Thanks to @RaymondChen for mentioning the Volume Shadow Copy service in the comments!

This is exactly what I need. Relating back to my example in my original post; after the audio files are linked to user's directories, my program can create a shadow copy of the volume so that after a change to a linked file, my program can restore the shadowed copy, and move the modified file to respective user's directory.

drdrez
  • 921
  • 1
  • 7
  • 16
  • VSS does not make a copy of every file. If the file does not change then there is no copy. – Raymond Chen Nov 05 '12 at 22:14
  • @RaymondChen Thank you again. I have to do a better job of understanding MSDN documentation. I will revise my answer to reflect that correction. – drdrez Nov 06 '12 at 02:53