This is an offshoot of a specific problem I've posted in the C# forum.

However, it applies to C++ as well.

If you have opened a file, specifically, an image file, but would like to rename (or, as MSDN says, 'move) the directory the file resides in, what are the requirements for being able to move the directory without the system complaining about access violations?

The imagefile I'm using is opened in a picturebox control, but making the picturebox show another image does not help. Only the complete commenting out of code that loads information about the picture seems to help, but it's necessary for me to be able to view the picture both before and after the move/renaming.

Is there a way to unload, detach, or close a file from an application such that the parent directory can be moved without problems?

How does the application/OS recognize that it cannot move the active file?

Thanks in advance.

Is there a way to unload, detach, or close a file from an application such that the parent directory can be moved without problems?

Yes. Refer the File Management Functions in MSDN.

How does the application/OS recognize that it cannot move the active file?

When an application opens the file, the programmer can set various sharing access rights so that the OS knows what to do when a request that affects the same file comes while that file is still open. You can see the list of flags that can be set under the Security Attributes section for CreateFile.

That looks to be just about what I'm interested in; however, with regards to the PictureBox control specifically, how would you force it to release the file that has been set to its image attribute?

Would CloseHandle do this?
Or should I focus on the Image, and call its dispose function before eliminating the reference in the picturebox control?
The latter seems more likely. I'll try that on monday.

As I (eventually) figured out, with inspiration from Wolfpack's reply, making sure to explicitly mark the image for closing and garbage collection solved the problem nicely.

Thanks for the inspiration, and I'll keep the file functions you linked in mind.

This question has already been answered. Start a new discussion instead.