首页 > 代码库 > MFC: Create Directory
MFC: Create Directory
Original link: How to check if Directory already Exists in MFC(VC++)?
MSDN Links:
CreateDirectory function
GetFileAttributes function
GetLastError function
System Error Codes
Example:
VC++ MFC DLL project
#include "stdafx.h" #include <windows.h> #include <Shellapi.h> // To check if a file/folder exists: // Method 1: GetFileAttributes // Description: // Retrieves file system attributes for a specified file or directory. // Return value: // a) If the function succeeds, the return value contains the attributes of the specified file or directory. For a list of attribute values and their descriptions, see File Attribute Constants. // b) If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError. if (GetFileAttributes(szDirPath) == INVALID_FILE_ATTRIBUTES) { CreateDirectory(szDirPath,NULL); } // Method 2: CreateDirectory // Description: // Creates a new directory // Return value: // a) If the function succeeds, the return value is nonzero. // b) If the function fails, the return value is zero. To get extended error information, call GetLastError. if(!CreateDirectory(szDirPath,NULL)) { if (GetLastError() == ERROR_ALREADY_EXISTS) { // directory already exists } else { // creation failed due to some other reasons } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。