简体   繁体   English

如何以编程方式复制在Windows下锁定的文件或文件夹?

[英]How do I copy a file or folder that is locked under windows programmatically?

What are the API calls to copy a file that is currently locked. 什么是API调用来复制当前锁定的文件。 I'm hoping to be able to use .Net, but Win32 calls would be fine as well. 我希望能够使用.Net,但Win32调用也没问题。

Please feel free to chime in about the same functionality on Unix, or any other OS. 请随意在Unix或任何其他操作系统上使用相同的功能。

You can use the VSS (Volume Shadow Copy Service, not Visual SourceSafe) API for this purpose. 您可以使用VSS(卷影复制服务,而不是Visual SourceSafe)API来实现此目的。 While powerful, this isn't exactly an easy-to-use API: the Overview of Processing a Backup Under VSS should give you an idea what's involved. 虽然功能强大,但这并不是一个易于使用的API: 在VSS下处理备份概述可以让您了解所涉及的内容。

Even though it's a relatively recent API, .NET support for VSS is pretty much (and inexcusably) nonexistent. 即使它是一个相对较新的API,但对VSS的.NET支持几乎(并且不可原谅地)不存在。 You can't call most of the API through Interop, and the Framework file functions won't work with the kernel namespace VSS uses to expose the snapshotted files. 您无法通过Interop调用大多数API,并且Framework文件函数将无法与VSS用于公开快照文件的内核命名空间一起使用。 As a bonus, there are horrendous 32/64-bit and XP-vs-Vista issues, making deployment exciting as well (the responsible team at Microsoft should be really proud!) 作为奖励,有可怕的32/64位和XP-vs-Vista问题,部署也令人兴奋(微软的负责团队应该感到非常自豪!)

Anyway, the AlphaVSS project intends to bring full VSS functionality to .NET, and looks extremely promising, even though it's still in pre-beta stage. 无论如何, AlphaVSS项目打算为.NET带来完整的VSS功能,看起来非常有前景,尽管它还处于测试前阶段。 It might just do the trick for you, though, and it's open source (Managed C++). 它可能只是为你做的,它是开源的(Managed C ++)。

For a good example of how to do things using Win32, see HoboCopy . 有关如何使用Win32执行操作的一个很好的示例,请参阅HoboCopy The utility is quite useful on its own, and full C++ source is available from the SourceForge project page as well. 该实用程序本身非常有用,并且SourceForge项目页面也提供了完整的C ++源代码。

Almost the same as my answer to another question : 几乎与我对另一个问题的回答相同:

If you are on Win32, the official way to do it is to mark it to be moved on reboot, and ask the user to reboot. 如果您使用的是Win32,那么正式的方法是将其标记为在重新启动时移动,并要求用户重新启动。 To mark the file to be moved on reboot, use MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT flag. 要在重新引导时标记要移动的文件,请使用带有MOVEFILE_DELAY_UNTIL_REBOOT标志的MoveFileEx

It's the same function, only this time you don't pass NULL as the destination. 它是相同的功能,只是这次你没有传递NULL作为目的地。

Depending upon what exactly has locked your file, you can either do System.IO.File.Copy(), or create a System.IO.BinaryReader and a System.IO.BinaryWriter and manually create a copy of the file by reading chunks of the locked file and writing them to a new file. 根据确切锁定文件的内容,您可以执行System.IO.File.Copy(),也可以创建System.IO.BinaryReader和System.IO.BinaryWriter,并通过读取块来手动创建文件的副本。锁定的文件并将它们写入新文件。 I have seen situations where one method was possible but the other wasn't, based on why the file was locked. 我已经看到了一种方法是可能的,但另一种方法不是,基于文件被锁定的原因。

This is the "all .NET" answer. 这是“所有.NET”的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM