I'm certainly no expert in programming on the Win32 API, but what I just learned is that using the CreateFile
function I can mark it to be deleted by the operating system once its handle is released using the FILE_FLAG_DELE开发者_JAVA百科TE_ON_CLOSE
value. I can't seem to find a way to do the same with using the CreateDirectory
, or have I missed something?
Optionally, can I subscribe to changes within this specific folder (such as new files created) and then mark all files within the folder with this flag somehow?
You can open a handle to a directory by using the FILE_FLAG_BACKUP_SEMANTICS flag:
#include <windows.h>
#include <stdio.h>
void main()
{
CreateDirectory(".\\testdir",NULL);
system("pause");
HANDLE hDir = CreateFile(".\\testdir",0,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,0,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_DELETE_ON_CLOSE,NULL);
CloseHandle(hDir);
}
This did work on the machine I tested on (if the directory is empty) but I'm not sure if it is documented as a valid thing to do.
精彩评论