【问题标题】:Set File attribute succed but retriving it fails设置文件属性成功但返回失败
【发布时间】:2017-05-05 08:55:02
【问题描述】:

我使用 SetFileAttributesW 设置自定义文件属性,例如0x200008,并且SetFileAttributesW返回非零表示没有错误。但 GetFileAttributesW 检索文件属性忽略设置属性。

int main()
{
    uint32_t magic = 0x200008;
    DWORD attribute = GetFileAttributesW(L"test");
    cout << attribute << endl;
    if ((attribute & magic) == magic)
        cout << "has magic" << endl;
    else
    {
        attribute |= magic;
        cout << attribute << endl;
    }
    cout << SetFileAttributesW(L"test", attribute) << " " << GetLastError();
    cin.get();
    return 0;
}

以及每次运行的输出
32
2097192
1 0

谁能帮帮我?

最好的。

【问题讨论】:

    标签: c++ windows file-attributes


    【解决方案1】:

    没有任何属性有效,如果你查看 wmd.h 你可以找到下一个定义:

    #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
    
    #define FILE_ATTRIBUTE_VALID_FLAGS          0x00007fb7
    #define FILE_ATTRIBUTE_VALID_SET_FLAGS      0x000031a7
    
    #else
    
    #define FILE_ATTRIBUTE_VALID_FLAGS          0x0002ffb7
    #define FILE_ATTRIBUTE_VALID_SET_FLAGS      0x000231a7
    
    #endif
    

    所以您的自定义文件属性 0x200008 无效并且将是未定义的行为。但是在当前的实现中,文件系统会忽略它们不理解的任何值,而是返回错误 见FASTFAT source

        //
        //  Only permit the attributes that FAT understands.  The rest are silently
        //  dropped on the floor.
        //
    
    
    
        Attributes = (UCHAR)(Buffer->FileAttributes & (FILE_ATTRIBUTE_READONLY |
                                                       FILE_ATTRIBUTE_HIDDEN |
                                                       FILE_ATTRIBUTE_SYSTEM |
                                                       FILE_ATTRIBUTE_DIRECTORY |
                                                       FILE_ATTRIBUTE_ARCHIVE));
    

    NTFS 做同样的事情。

    所以只使用在 windows 头文件和掩码中声明的属性 FILE_ATTRIBUTE_VALID_SET_FLAGS

    【讨论】:

      【解决方案2】:

      您使用的值似乎不受支持。 Microsoft 未记录 0x200008。

      查看支持的常量:[https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx][1]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2019-03-13
        • 2011-10-16
        • 1970-01-01
        • 2015-05-25
        相关资源
        最近更新 更多