watson
watson

Reputation: 53

Problems with directory deletion MFC

I am doing a mfc program in which i have created a directory to store dicom images using

 CreateDirectory(Directory_name,NULL);

and succesfully created the folder, After that i have used this folder of dicom images to perform volume rendering and it goes well too.

After that when i try to delete the directory including the dicom files ,everything is deleted except the last dicom file of the folder which i have given to the volume rendering process. What is the concept going on here? How do i delete it?

Upvotes: 0

Views: 178

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490178

Under Windows, you can't delete a file while it's open.

If you're using Windows functions directly, and specifically, opening the files with CreateFile, you can pass FILE_FLAG_DELETE_ON_CLOSE to (obviously enough) have each file deleted as soon as it's closed.

If (far more likely) you're using almost any other way to open the files (iostreams, FILE *s, CFile, etc.) you're pretty much stuck with the fact that you'll need to close the files before you can delete them. In the case of the file being open in a child process, you'll typically need to wait for that process to finish and close the file before you try to delete the files.

Again, your options there depend somewhat on how you create the child process. If you call CreateProcess directly, you'll get a handle to the child process. That handle will be signaled when the child process exits, so you can do something like WaitForSingleObject on the handle, and when it returns success, you know the child process has exited. Most other methods of spawning the child require at least slightly more roundabout methods though (and, like with opening files, it's far more common to use other methods than to call CreateProcess directly--understandably, since using CreateProcess directly can be a bit of a pain).

Upvotes: 3

Related Questions