Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I'm hoping to be able to use .Net, but Win32 calls would be fine as well.

Please feel free to chime in about the same functionality on Unix, or any other OS.

like image 268
Steve T Avatar asked Nov 03 '08 16:11

Steve T


People also ask

How do I copy a locked file in Windows?

You can copy a locked file from a disk image or hard drive by using the file system browser in OSForensics. In the file system browser choose "Add device to case" from the File menu and select the drive letter or image file you wish to copy from.

Can you copy an open file?

No you can't. Some programs won't lock the file for reading, but if they do, you can't get to the file without killing the other program.

How do you check if a file is locked by another process in Windows?

In the Resource Monitor window, go to the CPU tab and expand the Associated Handles option. Now, in the search box, type the name of the file that is showing locked by a process and press Enter button. It will show you a list of processes holding the target file.


1 Answers

You can use the VSS (Volume Shadow Copy Service, not Visual SourceSafe) API for this purpose. 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.

Even though it's a relatively recent API, .NET support for VSS is pretty much (and inexcusably) nonexistent. 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. 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!)

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. It might just do the trick for you, though, and it's open source (Managed C++).

For a good example of how to do things using Win32, see HoboCopy. The utility is quite useful on its own, and full C++ source is available from the SourceForge project page as well.

like image 163
mdb Avatar answered Oct 04 '22 12:10

mdb