CreateDirectory function (Windows)

Switch View :
ScriptFree
CreateDirectory function

Applies to: desktop apps | Metro style apps

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.

Syntax

BOOL WINAPI CreateDirectory(
  __in      LPCTSTR lpPathName,
  __in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

Parameters

lpPathName [in]

The path of the directory to be created.

There is a default string size limit for paths of 248 characters. This limit is related to how the CreateDirectory function parses paths.

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.

lpSecurityAttributes [in, optional]

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.)

Return value

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.

Return codeDescription
ERROR_ALREADY_EXISTS

The specified directory already exists.

ERROR_PATH_NOT_FOUND

One or more intermediate directories do not exist; this function will only create the final directory in the path.

 

Remarks

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.

Examples

For an example, see Retrieving and Changing File Attributes.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

CreateDirectoryW (Unicode) and CreateDirectoryA (ANSI)

See also

CreateDirectoryEx
CreateDirectoryTransacted
CreateFile
Creating and Deleting Directories
Directory Management Functions
RemoveDirectory
SECURITY_ATTRIBUTES
SECURITY_INFORMATION

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Community Content

Vladimir Belov
Close

Close


Michael Brüstle
ERROR_ALREADY_EXISTS means that the lpPathName is in use.

So don't expect that you can use the lpPathName as a directory. It can be also an ordinary file!


dmex
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool CreateDirectory(string lpPathName, SECURITY_ATTRIBUTES lpSecurityAttributes);

dmex
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function CreateDirectory(ByVal lpPathName As String, ByVal lpSecurityAttributes As SECURITY_ATTRIBUTES) As Boolean
End Function

HanyF
Incorrect annotations on parameters
The lpSecurityAttributes is the optional parameter, whereas the lpPathName parameter cannot be null, so the annotations are reversed.

Gideon7
Bypass security
Note: CreateDirectory and CreateDirectoryEx will bypass the security of the parent folder if SeRestorePrivilege is enabled.

ddaS-edEn
Creating intermediate directories?
Note: The functions CreateDirectory & CreateDirectoryEx - creates only the final directory in the path. To create all intermediate directories on the path, use the SHCreateDirectoryEx() function [http://msdn2.microsoft.com/en-us/library/ms647698.aspx].