我想用临时目录中的另一个目录完全替换文件系统上的一个目录。棘手的是要替换的文件夹中的文件随时可能被使用,导致替换操作失败。
我需要以某种方式等待目录的独占锁,以便我可以删除它的所有内容而不会失败,这样我就可以移动另一个目录来替换它。
使事情变得更加困难的是,可能使用这些文件的过程是我自己的(通过 Lucene.net 库,并且不在我的手中)。所以它不能是进程级锁,它必须是对象级锁。
关于如何执行此操作的任何想法?还是我应该继续重新尝试直到成功?我想这始终是一个选择。
最佳答案
Microsoft 的 InUse.exe
实用程序可以完成此操作,但是它需要重新启动计算机才能使更改生效。更多信息:http://support.microsoft.com/kb/228930
示例用法:
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.
关于c# - 如何确保我可以替换目录中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2814752/