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_PATHNAME
The pszPath parameter was set to a relative path.
ERROR_FILENAME_EXCED_RANGE
The path pointed to by pszPath is too long.
ERROR_FILE_EXISTS
the directory exists.
ERROR_ALREADY_EXITSTS
the directory exists.
ERROR_CANCELLED
The user canceled the operation.
備考:1.CreateDirectoryと比較して、この関数は多層フォルダを作成できます.
注意:1.以上の2つの関数は、作成したフォルダのパスがMAX_を超えてはいけないことを制限しています.PATH文字、この制限はこの関数がどのようにパスを解析するかに関係します.2、以上の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-失敗.
errno
content
EEXIST
Directory was not created because dirname is the name of an existing file, directory, or device.
ENOENT
Path 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-失敗.
errno
content
ENOTEMPTY
Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.
ENOENT
Path was not found.
EACCES
A 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.