通过Win32 API获取文件和文件夹的ACL

本文关键字:文件夹 ACL 文件 Win32 API 获取 通过 | 更新日期: 2023-09-27 18:05:49

哪一个PInvoke我需要验证权限(CanRead, CanWrite, CanExecute…)为一个UNC路径('UNC'?'还是'?', Files and Folders)。与系统。我想用fileInfo.GetAccessControll().GetAccessRules来得到AuthorizationRuleCollection,但是我不能用System。

我知道如何获得所有者,但我没有找到其他信息的解决方案。我想我也必须使用GetNamedSecurityInfo,但是信息非常稀疏。

谢谢。

通过Win32 API获取文件和文件夹的ACL

解决方案是使用GetNamedSecurityInfo和参数pSecurityDescriptor以及DACL信息请求。

// Get Length
var securityDescriptorLength = /* Win32 Call */ GetSecurityDescriptorLength( pSecurityDescriptor );
// Define array to copy
var securityDescriptorDataArray = new byte[ securityDescriptorLength ];
// Copy by marshal to defined array
/* Win32 Call */ Marshal.Copy( pSecurityDescriptor, securityDescriptorDataArray, 0, ( int ) securityDescriptorLength );
// If path is directory
var securityInfo = new DirectorySecurity( );
securityInfo.SetSecurityDescriptorBinaryForm( securityDescriptorDataArray );

现在你可以使用securityInfo.GetAccessRules()

获取AccessRules