CreateDirectoryW function (fileapi.h)
Creates a new directory. If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
To specify a template directory, use the CreateDirectoryEx function.
To perform this operation as a transacted operation, use the CreateDirectoryTransacted function.
BOOL CreateDirectoryW( [in] LPCWSTR lpPathName, [in, optional] LPSECURITY_ATTRIBUTES lpSecurityAttributes );
The path of the directory to be created.
For the ANSI version of this function, there is a default string size limit for paths of 248 characters (MAX_PATH - enough room for a 8.3 filename). To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File
[in, optional] lpSecurityAttributes
A pointer to a SECURITY_ATTRIBUTES structure. The lpSecurityDescriptor member of the structure specifies a security descriptor for the new directory. If lpSecurityAttributes is NULL, the directory gets a default security descriptor. The ACLs in the default security descriptor for a directory are inherited from its parent directory.
The target file system must support security on files and directories for this parameter to have an effect. (This is indicated when GetVolumeInformation returns FS_PERSISTENT_ACLS.)
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError. Possible errors include the following.
||The specified directory already exists.|
||One or more intermediate directories do not exist; this function will only create the final directory in the path.|
Some file systems, such as the NTFS file system, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent directory.
An application can obtain a handle to a directory by calling CreateFile with the FILE_FLAG_BACKUP_SEMANTICS flag set. For a code example, see CreateFile.
To support inheritance functions that query the security descriptor of this object may heuristically determine and report that inheritance is in effect. See Automatic Propagation of Inheritable ACEs for more information.
In Windows 8 and Windows Server 2012, this function is supported by the following technologies.
|Server Message Block (SMB) 3.0 protocol||Yes|
|SMB 3.0 Transparent Failover (TFO)||Yes|
|SMB 3.0 with Scale-out File Shares (SO)||Yes|
|Cluster Shared Volume File System (CsvFS)||Yes|
|Resilient File System (ReFS)||Yes|
For an example, see Retrieving and Changing File Attributes.
The fileapi.h header defines CreateDirectory as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.
|Minimum supported client||Windows XP [desktop apps | UWP apps]|
|Minimum supported server||Windows Server 2003 [desktop apps | UWP apps]|
|Header||fileapi.h (include Windows.h)|
Creating and Deleting Directories
Directory Management Functions