DerAbt
DerAbt

Reputation: 347

C# PInvoke to get ACL of files and folders by Win32 API

which PInvoke do I need to verify the permissions (CanRead, CanWrite, CanExecute...) for an UNC-Path(\UNC\?\ or \?\, Files and Folders). With System.IO I would use fileInfo.GetAccessControll().GetAccessRules to get the AuthorizationRuleCollection but I can't work with System.IO, because this namespace does not support long paths.

I know how to get the owner, but I found no solution for the other information. I thought I have to use GetNamedSecurityInfo as well but the information are very sparse.

Thanks.

Upvotes: 2

Views: 1367

Answers (1)

DerAbt
DerAbt

Reputation: 347

The solution is to use GetNamedSecurityInfo and the Parameter pSecurityDescriptor and the DACL information request.

// 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 );

Now you can get the AccessRules by using securityInfo.GetAccessRules()

Upvotes: 2

Related Questions