2

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.

devios1
  • 36,899
  • 45
  • 162
  • 260

3 Answers3

1

SLaks's answer is wrong, you cannot rename the entire directory if it contains files that are in use.

However, you can rename or move files that are in use. I don't know if that works in all versions of Windows, but I know it certainly does in Windows 7. If a program has the file open, it will stay open, but if something tries to open the file after the move, it obviously won't be there any more.

So you can actually use this to replace a running executable, but if the executable tries to load one of the .dlls that it depended on which also got moved, the application will crash on the end user.

Depending on what you're trying to replace, you could create a directory for eventual deletion, move each file that is to be replaced into that directory, copy the new file in, and when you're done, attempt to delete each file in the temp directory. Then if there's any files left, you could mark them for eventual deletion with this:

"MoveFile" function in C# (Delete file after reboot)


After further experimentation, I've discovered that, you can move files that are in use, but only under certain circumstances. If you open the file with "ShareDelete" file access it works. It also appears to work with running executables and dlls... they can be moved even if they can't be deleted. But a file that was opened normally (such as with "File.OpenRead()") will still give a "file in use" IOException.

Community
  • 1
  • 1
Bryce Wagner
  • 2,640
  • 1
  • 26
  • 43
1

The InUse.exe utility from Microsoft will accomplish this, however it requires that the computer be restarted for the changes to take effect. More information: http://support.microsoft.com/kb/228930

Example usage:

c:\Program Files (x86)\Resource Kit>inuse C:\temp\new C:\temp\old /y
InUse - version 1.4
---------------------------------------------------------------------------
Copyright (c) 1994-1999 Microsoft Corporation. All rights reserved
Windows 2000 detected - WFP is enforced
Confirmation suppressed

INUSE is about to replace the following file

        Existing:    C:\temp\old
        No version info available

        Replacement: C:\temp\new
        No version info available

        C:\temp\new is replacing --> C:\temp\old

Changes will not take affect until you reboot.
umbyersw
  • 585
  • 5
  • 12
  • Hmm, that's interesting. Unfortunately requiring a restart makes this not feasible for me, but thanks anyway. – devios1 Jul 11 '10 at 22:35
0

If you're replacing the entire directory, you can rename the original directory, then rename to new one to the original name.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Is this actually possible? I assumed that would fail if files inside the directory were locked. – devios1 May 11 '10 at 22:09
  • 1
    If the files are in use, you need to unlock them first. There is no way to modify or replace a locked file without unlocking it. – SLaks May 11 '10 at 22:10
  • It seems to me if I could just rename the directory, I wouldn't have any trouble replacing it to begin with. Ideally I'd like to just wait for any other usage to cease before deleting it, but are you saying there's a way I can force it to be deleted even if files in it are in use elsewhere? – devios1 May 11 '10 at 22:18
  • Not that I know of. You could forcibly close that handles, but that's probably not a good idea. – SLaks May 11 '10 at 22:22