c/c++ 检查指定目录是否存在,不存在则创建改目录

 1 #include 
 2 #include <string> 
 3 #include 
 4 using namespace std; 
 5 
 6 // 核查目录,若目录不存在,创建目录
 7 bool FindOrCreateDirectory( const char* pszPath )
 8 {
 9     WIN32_FIND_DATA fd;
10     HANDLE hFind = ::FindFirstFile( pszPath, &fd );
11     while( hFind != INVALID_HANDLE_VALUE )
12     {
13         if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
14             return true;
15     }
16 
17     if ( !::CreateDirectory( pszPath, NULL ) )
18     {
19         char szDir[MAX_PATH];
20         sprintf_s( szDir, sizeof(szDir), "创建目录[%s]失败,请检查权限", pszPath );
21         ::MessageBox( NULL, szDir, "创建目录失败", MB_OK|MB_ICONERROR );
22         return false;
23     }
24 
25     return true;
26 }

 

你可能感兴趣的:(c/c++ 检查指定目录是否存在,不存在则创建改目录)