C、C++创建和删除文件夹操作

创建文件夹

1、调用Window API函数

BOOL CreateDirectory(LPCTSTR lpPathName,
                     LPSECURITY_ATTRIBUTES lpSecurityAttributes);

头、库文件
Winbase.h, Coredll.lib

参数:
- lpPathName:字符串指针,要创建的文件夹的路径。
- lpSecurityAttributes:忽略,设置为NULL。

返回值:
成功-非零,失败-零。查看错误使用GeLastError();

备注:
1. 该函数不能创建多层文件夹。
2. 该函数是同步操作,对应的异步操作函数为CeCreateDirectory


int SHCreateDirectory(HWND hwnd, 
                      LPCWSTR pszPath);

头、库文件
shlobj.h,shell32.lib

参数:
- hwnd:父窗口的句柄。一般设置为空。
- pszPath:字符串指针,要创建的文件夹的路径。

返回值:
ERROR_SUCCESS-成功

返回值内容
ERROR_BAD_PATHNAMEThe pszPath parameter was set to a relative path.
ERROR_FILENAME_EXCED_RANGEThe path pointed to by pszPath is too long.
ERROR_FILE_EXISTSthe directory exists.
ERROR_ALREADY_EXITSTSthe directory exists.
ERROR_CANCELLEDThe user canceled the operation.

备注:
1. 相比CreateDirectory,该函数可以创建多层文件夹。

注意:
1. 以上两个函数都限制创建的文件夹的路径不能超过MAX_PATH个字符,这限制跟该函数如何解析路径有关。
2、以上两个函数都是对应的Ex版本,CreateDirectoryEx, SHCreateDirectoryEx.

2、调用C运行库函数

int _mkdir(const char* dirname);

int _wmkdir(const wchar_t* dirname);

头、库文件:
direct.h或者wchar.h,c运行库

宏定义:

tchar.h_MBCS defined_UNICODE defined
_tmkdir_mkdir_wmkdir

参数:
- dirname:字符串指针,要创建的文件夹的路径。

返回值:
0-成功,-1-失败。

errnocontent
EEXISTDirectory was not created because dirname is the name of an existing file, directory, or device.
ENOENTPath was not found.

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

删除文件夹

1、调用Windows API函数

BOOL RemoveDirectory(LPCTSTR lpPathName);

头、库文件
Winbase.h, Coredll.lib

参数:
- lpPathName:字符串指针,要删除的文件夹的路径。

返回值:
成功-非零,失败-零。查看错误使用GeLastError();

备注:
1、RemoveDirectory only operates on empty directories. If the directory is not empty, an application must first use FindFirstFile, FindNextFile, RemoveDirectory and DeleteFile to recursively enumerate and delete the files and subdirectories inside the directory.

2、调用C运行库函数

int _rmdir(const char* dirname);

int _wrmdir(const wchar_t* dirname);

头、库文件:
direct.h或者wchar.h,c运行库

宏定义:

tchar.h_MBCS defined_UNICODE defined
_trmdir_rmdir_wrmdir

参数:
- dirname:字符串指针,要删除的文件夹的路径。

返回值:
0-成功,-1-失败。

errnocontent
ENOTEMPTYGiven path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.
ENOENTPath was not found.
EACCESA program has an open handle to the directory.

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注:
1、The directory must be empty, and it must not be the current working directory or the root directory.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值