溫馨提示×

c++怎么修改文件夾的用戶訪問權限

c++
小億
244
2023-12-13 13:02:29
欄目: 編程語言

在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++,你需要使用該操作系統提供的相應函數來修改文件夾的訪問權限。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女