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

标签 c# winapi pinvoke acl .net

我需要哪个 PInvoke 来验证 UNC 路径(\UNC\?\或\?\,文件和文件夹)的权限(CanRead、CanWrite、CanExecute...)。 对于 System.IO,我将使用 fileInfo.GetAccessControll().GetAccessRules 来获取 AuthorizationRuleCollection 但我无法使用 System.IO,因为此命名空间不支持 long路径。

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

谢谢。

最佳答案

解决方案是使用 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

关于C# PInvoke 通过 Win32 API 获取文件和文件夹的 ACL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634475/

相关文章:

javascript - SignalR 更改中心订阅

c# - linq to entities 无法将类型 Icollection 转换为类型 List

c# - 函数的 IDL 声明(在 C++ 中)将从 C# 获取 C 样式数组

c++ - 在调整大小时 move 对话框上的按钮

c++ - WIN API 用户权限 C++

c# - 使用dll导入

c# - 通用类型的默认值?

c# - 具有部分已知标题的 FindWindow

c# - 将 C 结构编码到 C#

c++ - std::unique_ptr、删除器和 Win32 API