判断一个文件夹是否存在

//判断该目录是否存在
HANDLE hDir = ::CreateFile(strFolderPath, GENERIC_READ, FILE_SHARE_READ,
	NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (INVALID_HANDLE_VALUE == hDir)
{
	AfxMessageBox(_T("不存在的路径"));
}
else
{
	CloseHandle(hDir);
	ListFilesAndFolders(strFolderPath);
}

最重要的是要指定FILE_FLAG_BACKUP_SEMANTICS标识:

An application cannot create a directory by using CreateFile. The application must call CreateDirectory or CreateDirectoryEx to create a directory. To open a directory by using CreateFile, use the FILE_FLAG_BACKUP_SEMANTICS flag.

When using CreateFile to open a directory during defragmentation of a FAT or FAT32 file system volume, do not specify the MAXIMUM_ALLOWED access right. Access to the directory is denied if this is done. Specify the GENERIC_READ access right instead.

 


2012-4-17更新:

BOOL PathIsDirectory(          LPCTSTR pszPath
);
BOOL PathIsDirectoryEmpty(          LPCTSTR pszPath
);
posted @ 2012-03-26 13:37  kcy-666  阅读(894)  评论(0编辑  收藏  举报