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 thought开发者_如何学JAVAs on how I might do this? Or should I just keep re-attempting until it succeeds? I guess that's always an option.
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.
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.
If you're replacing the entire directory, you can rename the original directory, then rename to new one to the original name.
精彩评论