コード例 #1
0
ファイル: File.cs プロジェクト: bangush/LongPath
        public static FileSecurity GetAccessControl(string path, AccessControlSections includeSections)
        {
            if (Common.IsRunningOnMono() && Common.IsPlatformUnix())
            {
                return(SysFile.GetAccessControl(path, includeSections));
            }


            var normalizedPath = Path.NormalizeLongPath(Path.GetFullPath(path));

            IntPtr        SidOwner, SidGroup, Dacl, Sacl, ByteArray;
            SecurityInfos SecurityInfos =
                Common.ToSecurityInfos(includeSections);

            int errorCode = (int)NativeMethods.GetSecurityInfoByName(normalizedPath,
                                                                     (uint)ResourceType.FileObject,
                                                                     (uint)SecurityInfos,
                                                                     out SidOwner,
                                                                     out SidGroup,
                                                                     out Dacl,
                                                                     out Sacl,
                                                                     out ByteArray);

            Common.ThrowIfError(errorCode, ByteArray);

            uint Length = NativeMethods.GetSecurityDescriptorLength(ByteArray);

            byte[] BinaryForm = new byte[Length];

            Marshal.Copy(ByteArray, BinaryForm, 0, (int)Length);

            NativeMethods.LocalFree(ByteArray);
            var fs = new FileSecurity();

            fs.SetSecurityDescriptorBinaryForm(BinaryForm);
            return(fs);
        }