/// <summary> /// Gets the security information of specified handle from file system /// </summary> /// <param name="sidHandle">Handle to get file security information</param> /// <returns><see cref="CommonObjectSecurity"/>Result</returns> private CommonObjectSecurity ReceiveFileSystemSecurityInformation(out IntPtr sidHandle) { IntPtr zeroHandle = new IntPtr(); IntPtr pSecurityDescriptor = new IntPtr(); try { var namedSecInfoResult = Win32SafeNativeMethods.GetNamedSecurityInfo(PathInfo.FullNameUnc, Win32SecurityObjectType.SeFileObject, Win32FileSystemEntrySecurityInformation.OwnerSecurityInformation | Win32FileSystemEntrySecurityInformation.DaclSecurityInformation, out sidHandle, out zeroHandle, out zeroHandle, out zeroHandle, out pSecurityDescriptor); var win32Error = Marshal.GetLastWin32Error(); // Cancel if call failed if (namedSecInfoResult != 0) { Win32ErrorCodes.NativeExceptionMapping(PathInfo.FullName, win32Error); } var securityDescriptorLength = Win32SafeNativeMethods.GetSecurityDescriptorLength(pSecurityDescriptor); var securityDescriptorDataArray = new byte[securityDescriptorLength]; Marshal.Copy(pSecurityDescriptor, securityDescriptorDataArray, 0, ( int )securityDescriptorLength); CommonObjectSecurity securityInfo; if (PathInfo.Attributes.Contains(FileAttributes.Directory)) { securityInfo = new DirectorySecurity(); securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray); } else { securityInfo = new FileSecurity(); securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray); } return(securityInfo); } finally { Win32SafeNativeMethods.LocalFree(zeroHandle); Win32SafeNativeMethods.LocalFree(pSecurityDescriptor); } }