问题描述
在win32 c ++中;有没有一种方法可以确定是否可以访问文件夹/文件?您知道如何尝试访问C:/Windows目录&中的某个文件夹.您将看到一个弹出窗口,提示此文件夹不可访问".
In win32 c++; is there a way to determine if a folder/file is accessible? You know how if you try to access a certain folder in the C:/Windows directory & you will get a popup saying "This folder is not accessible".
也许有一个文件属性常量表示文件是私有的?也许像FILE_ATTRIBUTE_PRIVATE之类的东西?
Maybe there is a file attribute constant that signifies that the file is private? Maybe something like FILE_ATTRIBUTE_PRIVATE?
WIN32_FIND_DATA dirData;
while (FindNextFile( dir, &dirData ) != 0 )
{
// I made the following constant up
if ( !(fileData.dwFileAttributes & FILE_ATTRIBUTE_PRIVATE) )
{
// file is accessible so store filepath
files.push_back( fileData.cFileName );
}
else // file is not accessible so dont store
}
或者是唯一知道的方法:
Or is the only way to know by going:
dir = FindFirstFileEx( (LPCTSTR)directory.c_str(), FindExInfoStandard, &dirData, FindExSearchNameMatch, NULL, 0 );
if ( dir == ??? ) { the file is inaccessible } [/code]
推荐答案
这不是文件本身的标志,因为不同的帐户可能有权访问不同的文件/目录.相反,Windows使用ACL(访问控制列表),它们是确定谁有权访问内容的数据结构.
It wouldn't be a flag on the file itself because different accounts may have access to different files/directories. Instead, windows uses ACL's (access control lists), which are data structures that determine who has access to what.
ACL几乎可以与句柄引用的任何内容(文件,目录,进程,互斥体,命名管道...)一起使用.您可以通过转到文件的属性并查看安全性"选项卡来查看文件ACL.
ACLs in windows can be used with just about anything that is referred to by a handle (files, directories, processes, mutexes, named pipes...). You can view file ACLs by going to properties of a file and view "Security" tab.
因此,在您的应用程序中,您实际上并不是要检查标志,而是将文件的ACL与运行应用程序的用户帐户进行比较.查看 AccessCheck Win32函数.我认为这正是您想要的.
So in your app you don't really want to check for a flag, but to compare file's ACL against the user account under which your app is running. Check out AccessCheck Win32 function. I think it's exactly what you are looking for.
就我个人而言,我从未使用过该函数,但是如果您正在寻找Win32解决方案并且想要一个函数调用,那可能是最好的选择.但是,正如其他人指出的那样,它可能太复杂了.我一直使用_access(或_waccess),它是CRT的一部分,非常易于使用,并且您不会因为仅关闭文件而获取文件句柄而导致性能下降(取决于循环的紧密程度,这些调用可以加起来).
Personally, I've never used that function, but if you are looking for Win32 solution and you want a function call, that's probably your best bet. However, as others have pointed out, it might be too complicated. I've always used _access (or _waccess) which is part of CRT, uber easy to use, and you don't take a performance hit of acquiring a file handle only to close it (depending on how tight your loop is, those calls can actually add up).
这篇关于WinAPI确定文件是否可访问/私有的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!