December 3rd, 2025
0 reactions

How do I check whether the user has permission to create files in a directory?

A customer wanted to accept a directory entered by the user and verify that the user has permission to create files in that folder. The directory itself might not even be on a local hard drive; it could be a DVD or a remote network volume. They tried calling Get­File­Attributes, but all they were told was that it was a directory.¹ How can they find out whether the user can create files in it?

The file attributes are largely legacy flags carried over from MS-DOS. The actual control over what operations are permitted comes not from the file attributes but from the security attributes.

Fortunately, you don’t have to learn how to parse security attributes. You can just specify the desired attributes when you open the file or directory. In other words, to find out if you can do the thing, ask for permission to do the thing.

The security attribute that controls whether users can create new files in a directory is FILE_ADD_FILE. You can find a complete list in the documentation under File Access Rights Constants.

Directories are a little tricky because you have to open them with backup semantics.

bool HasAccessToDirectory(PCWSTR directoryPath, DWORD access)
{
    HANDLE h = CreateFileW(directoryPath, access,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
        OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
    if (h == INVALID_FILE_HANDLE) {
        return false;
    } else {
        CloseHandle(h);
        return true;
    }
}

bool CanCreateFilesInDirectory(PCWSTR directoryPath)
{
    return HasAccessToDirectory(directoryPath, FILE_ADD_FILE);
}

You can choose other access flags to detect other things. For example, checking for FILE_ADD_SUBDIRECTORY checks whether the user can create subdirectories, and checking for FILE_DELETE_CHILD checks whether the user can delete files and remove subdirectories from that directory. If you want to check multiple things, you can OR them together, because security checks require that you be able to do all of the things you requested before it will let you in.

bool CanCreateFilesAndSubdirectoriesInDirectory(PCWSTR directoryPath)
{
    return HasAccessToDirectory(directoryPath,
                FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY);
}

Note that these are moment-in-time checks. You will have to be prepared for the possibility that the user has lost access by the time you actually try to perform the operation. But this will at least give you an opportunity to tell the user up front, “You don’t have permission to create files in this folder. Pick another one.”²

As I noted, this technique applies to files as well. If you want to know if the user can write to a file, open it for writing and see if it succeeds!

¹ And we learned some time ago that the read-only attribute on directories doesn’t actually make the directory read-only.

² This could be handy if the act of creating the files happens much later in the workflow. For example, maybe you’re asking the user where to save the query results. The query itself might take a long time, so you don’t want to let the user pick a directory, and then 30 minutes later, put up a dialog box saying “Oops, I couldn’t save the files in that directory. Maybe you should have picked a better one 30 minutes ago.”

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments