Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
createdirectory (kernel32)

C# Signature:

[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CreateDirectory(string lpPathName,
   IntPtr lpSecurityAttributes);

User-Defined Types:



Advantage - Doesn't require the running assembly to have permissions to the entire folder path.

Disadvantage - Only creates the end-most directory, not the full path.

Tips & Tricks:

Iteratively Create a Directory Tree

I updated the create directory code (LongPathDirectory.Create()) code in the Microsoft.Experimental.IO project ( so some of the classes referenced in the code below are from that project.

    internal static class NativeMethods
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes);

    public static void Create(string path)
        if (String.IsNullOrEmpty(path))
        throw new ArgumentException("Path cannot be an empty string.");

        int startPos = 1;   // First part of the path to create.

        // Split the given string into components which will have to be individually created.
        String[] pathComponents = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);

        // Get the first path component, either "c:" or "\\servername".
        String partPath = pathComponents[0];

        // If the path is a UNC path, then add the share to the initial starting path.  
        if (partPath.StartsWith(@"\\"))
        if (pathComponents.Length < 3)
            throw new ArgumentException("Invalid path specified: " + path);

        // Add the share name.
        partPath = Path.Combine(partPath, pathComponents[1]);
        startPos = 2;

        // If the path is the root of a drive, then add the directoy separator char.
        if (partPath.Contains(":"))
        if (pathComponents.Length < 2)
            throw new ArgumentException("Invalid path specified: " + path);

        // Add the directory separator char here since Path.Combine will not add it if a drive specifier (ie: "C:") is given.
        // See Remarks in
        // Also see
        partPath += Path.DirectorySeparatorChar;

        // Create the individual path components.
        for (int i = startPos; i < pathComponents.Length; i++)
        // Add the path component to create.
        partPath = System.IO.Path.Combine(partPath, pathComponents[i]);

        string normalizedPath = LongPathCommon.NormalizeLongPath(partPath, "partPath");

        // Create the path.
        if (!NativeMethods.CreateDirectory(normalizedPath, IntPtr.Zero))
            // To mimic Directory.CreateDirectory, we don't throw if the directory (not a file) already exists
            int errorCode = Marshal.GetLastWin32Error();
            if (errorCode != NativeMethods.ERROR_ALREADY_EXISTS || !LongPathDirectory.Exists(partPath))
            throw LongPathCommon.GetExceptionFromWin32Error(errorCode);

Sample Code:

string path = "c:\\test\\newDir";
CreateDirectory(path, IntPtr.Zero);

Alternative Managed API:


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version