简体   繁体   English

检查Windows文件权限

[英]Checking windows file permissions

I'm trying to figure out how to check if a directory can be changed to without actually changing to the directory. 我正在试图弄清楚如何在不实际更改目录的情况下检查目录是否可以更改。

On a posix system I would do: 在posix系统上我会这样做:

if (access("/some/path", X_OK) == 0) {
  // okay!
}

If only Windows was so simple .... 如果只有Windows这么简单......

I've taken a look at the documentation or GetFileSecurity , but I can't seem to wrap my head around Windows ACLs and what I actually have to check for to ensure a directory can be changed to. 我已经看了一下文档或GetFileSecurity ,但我似乎无法围绕Windows ACL和我实际需要检查以确保目录可以更改为。

The only way to be sure if an operation will succeed is to actually try the operation. 确定操作是否成功的唯一方法是实际尝试操作。

However, SetCurrentDirectory will fail if you don't have FILE_TRAVERSE or SYNCHRONIZE permissions for the folder in question. 但是,如果您没有相关文件夹的FILE_TRAVERSESYNCHRONIZE权限, SetCurrentDirectory将失败。 So you can test this using CreateFile without actually changing the directory. 因此,您可以使用CreateFile对此进行测试,而无需实际更改目录。

bool TestForSetCurrentDirPermission(LPCWSTR pszDir)
{
    HANDLE hDir = CreateFile(pszDir, FILE_TRAVERSE | SYNCHRONIZE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS, NULL);

    if (hDir != INVALID_HANDLE_VALUE) CloseHandle(hDir);
    return hDir != INVALID_HANDLE_VALUE;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM