Microsoft Learn

CreateDirectoryA function

Syntax

BOOL CreateDirectoryA(
  LPCSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

Parameters

Parameter Description
lpPathName The path of the directory to be created. This string must include the drive, and optionally, a path and a file name.

This parameter can be an application-defined string.

The maximum length for this parameter is MAX_PATH, which is 260 characters. Use \\?\C:\very long path to bypass this limit.
lpSecurityAttributes A pointer to a SECURITY_ATTRIBUTES structure that specifies the security descriptor for the new directory. If this parameter is NULL, the directory receives a default security descriptor that is based on the parent directory's security descriptor.

Return value

Return Description
TRUE The directory was created successfully.
FALSE The directory was not created. To get extended error information, call GetLastError.

Remarks

To create a directory, the calling process must have the necessary access rights to the directory's parent directory.

If the directory already exists, the function succeeds and returns TRUE.

This function creates only one directory. To create intermediate directories in a path, you must call this function multiple times.

For Unicode support, use the CreateDirectoryW function.

Example

The following code example creates a directory named "MyNewDirectory" in the current directory.

#include <windows.h>
#include <iostream>

int main() {
    if (CreateDirectoryA("MyNewDirectory", NULL)) {
        std::cout << "Directory created successfully." << std::endl;
    } else {
        if (GetLastError() == ERROR_ALREADY_EXISTS) {
            std::cout << "Directory already exists." << std::endl;
        } else {
            std::cerr << "Error creating directory: " << GetLastError() << std::endl;
        }
    }
    return 0;
}

Requirements

| |
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Product Windows
Header fileapi.h (include windows.h)
Library Use Kernel32.lib
DLL Kernel32.dll