フォルダのファイルアクセス権限をC++で変更の方法

C++では、オペレーティングシステムが提供するシステムコール関数を使用してフォルダのユーザアクセス権を変更することができます。以下にC++とWindowsオペレーティングシステムを使用したサンプルコードを示します。

#include <iostream>
#include <Windows.h>

int main() {
    LPCWSTR folderPath = L"C:\\Path\\to\\Folder";

    // 获取文件夹的当前访问权限
    DWORD currentAttributes = GetFileAttributesW(folderPath);

    // 如果获取失败,输出错误消息并退出
    if (currentAttributes == INVALID_FILE_ATTRIBUTES) {
        std::cout << "Failed to get folder attributes. Error code: " << GetLastError() << std::endl;
        return 1;
    }

    // 修改访问权限为只读
    DWORD newAttributes = currentAttributes | FILE_ATTRIBUTE_READONLY;
    BOOL success = SetFileAttributesW(folderPath, newAttributes);

    // 如果修改失败,输出错误消息并退出
    if (!success) {
        std::cout << "Failed to set folder attributes. Error code: " << GetLastError() << std::endl;
        return 1;
    }

    std::cout << "Folder attributes successfully changed." << std::endl;

    return 0;
}

Windows オペレーティングシステムでのみ上記のコードを使用できることに注意してください。他のオペレーティングシステムで C++ を使用する場合は、適切な関数を使用して、オペレーティングシステムから提供されるディレクトリのアクセス権限を変更する必要があります。

bannerAds