c++ - Unable to make folder readonly - Stack Overflow

admin2025-04-18  2

I am trying to make a readonly folder and for that I am using the following code:

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

bool MakeReadonly(const char* folderPath)
{
    DWORD attrs = GetFileAttributesA(folderPath);
    return SetFileAttributesA(folderPath, attrs | FILE_ATTRIBUTE_READONLY) != 0;
}


int main() 
{
    std::string folderPath = "C:\\Users\\Username\\Desktop\\mydir";
    if (MakeReadonly(folderPath.c_str()))
    {
        std::cout << "Folder is readonly now";
    }
}

The MakeReadonly function returns true, but the mydir folder does not become readonly.

However, if you pass a path to a file instead of a folder to the MakeReadonly function, then this file will become readonly. What should be done to make this function work for a folder as well?

I am trying to make a readonly folder and for that I am using the following code:

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

bool MakeReadonly(const char* folderPath)
{
    DWORD attrs = GetFileAttributesA(folderPath);
    return SetFileAttributesA(folderPath, attrs | FILE_ATTRIBUTE_READONLY) != 0;
}


int main() 
{
    std::string folderPath = "C:\\Users\\Username\\Desktop\\mydir";
    if (MakeReadonly(folderPath.c_str()))
    {
        std::cout << "Folder is readonly now";
    }
}

The MakeReadonly function returns true, but the mydir folder does not become readonly.

However, if you pass a path to a file instead of a folder to the MakeReadonly function, then this file will become readonly. What should be done to make this function work for a folder as well?

Share asked Jan 30 at 14:08 Joe JJoe J 4839 bronze badges 9
  • 1 You would likely have to recursively make all files in the folder readonly – Mippy Commented Jan 30 at 14:12
  • 2 Did you read the documentation? «A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories. For more information, see "You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, or in Windows Vista. "» – Yksisarvinen Commented Jan 30 at 14:14
  • 1 @JoeJ Yes, that's what "not honoured" would mean in English. You can request that just fine, but it will not be respected by any program. – Yksisarvinen Commented Jan 30 at 14:19
  • 1 If you do a attrib /d C:\Users\Username\Desktop\mydir you should see that the R (read only) flag is set on the directory. (Assuming your user account has privileges to change the attributes of a directory or file at that location.) The R (read only) attribute flag has no effect on a directory, because the read only flag does not have any semantic meaning on a directory (it only has semantic meaning for file entries, not directory entries). – Eljay Commented Jan 30 at 14:35
  • 1 You can achieve what you want using the WinAPI Access Control Lists (file and folder security mechanism). This is more complicated and would need a separate question. – Richard Critten Commented Jan 30 at 14:58
 |  Show 4 more comments

1 Answer 1

Reset to default 4

You cannot make a directory read-only on windows. As @Yksisarvinen said, the read-only attribute is not honored on directories. If you want to make everything in the folder read-only, you could recursively set all the files in the folder to read-only, but this would not affect new files added to the folder.

Here is the code that recursively sets all files to read-only within a folder:

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

bool SetReadOnlyRecursive(const char* folderPath) 
{
    DWORD attrs = GetFileAttributesA(folderPath);
    if (attrs == INVALID_FILE_ATTRIBUTES) {
        std::cerr << "Failed to get attributes for folder: " << folderPath << std::endl;
        return false;
    }
    
    if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
        std::cerr << folderPath << " is not a valid folder." << std::endl;
        return false;
    }

    WIN32_FIND_DATA findFileData;
    HANDLE hFind = FindFirstFile((std::string(folderPath) + "\\*").c_str(), &findFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        std::cerr << "Failed to open folder for scanning: " << folderPath << std::endl;
        return false;
    }

    do {
        if (std::string(findFileData.cFileName) == "." || std::string(findFileData.cFileName) == "..") {
            continue;
        }

        std::string fullPath = std::string(folderPath) + "\\" + findFileData.cFileName;

        if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            if (!SetReadOnlyRecursive(fullPath.c_str())) {
                FindClose(hFind);
                return false;
            }
        } 
        else {
            if (!SetFileAttributesA(fullPath.c_str(), findFileData.dwFileAttributes | FILE_ATTRIBUTE_READONLY)) {
                std::cerr << "Failed to set read-only for file: " << fullPath << std::endl;
                FindClose(hFind);
                return false;
            }
        }
    } while (FindNextFile(hFind, &findFileData) != 0);

    FindClose(hFind);
    return true;
}

int main() 
{
    std::string folderPath = "C:\\Users\\Username\\Desktop\\mydir";
    if (SetReadOnlyRecursive(folderPath.c_str())) {
        std::cout << "Folder and its contents are read-only now." << std::endl;
    } else {
        std::cout << "Failed to set folder to read-only." << std::endl;
    }

    return 0;
}

转载请注明原文地址:http://anycun.com/QandA/1744914386a89412.html