How to ensure I can replace files in a directory?

Posted by chaiguy on Stack Overflow See other posts from Stack Overflow or by chaiguy
Published on 2010-05-11T22:01:06Z Indexed on 2010/05/11 22:04 UTC
Read the original article Hit count: 246

Filed under:
|
|
|
|

I want to completely replace one directory on the file system with another directory in a temp directory. The tricky part is that the files in the folder to be replaced could be being used at any time, causing the replace operation to fail.

I need to somehow wait on an exclusive lock on the directory so that I can delete all of its contents without failing, so I can then move the other directory in to replace it.

To make matters potentially more difficult, the process that is likely to be using the files is my own (via a Lucene.net library and out of my hands). So it can't be a process-level lock it has to be an object-level lock.

Any thoughts on how I might do this? Or should I just keep re-attempting until it succeeds? I guess that's always an option.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#