WinAPI way to determine if a file is accessible/private

前端 未结 3 1490
囚心锁ツ
囚心锁ツ 2021-01-22 13:04

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 pop

相关标签:
3条回答
  • 2021-01-22 13:31

    Best thing to do is just try to access it.

    You can calculate the access granted by the access control list for a particular user account, but this is quite complicated, and the permission could change after you do the access check. So just open the file and handle access denied errors.

    0 讨论(0)
  • int _access( 
       const char *path, 
       int mode 
    );
    

    Simple to use:

    http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx

    0 讨论(0)
  • 2021-01-22 13:56

    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.

    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.

    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.

    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).

    0 讨论(0)
提交回复
热议问题