示例#1
0
        private static bool InternalDirectoryExists(string uncPath)
        {
            int win32Error;
            var attrs = InternalQuickIO.SafeGetAttributes(uncPath, out win32Error);

            if (Equals(attrs, 0xffffffff))
            {
                return(false);
            }
            if (InternalHelpers.ContainsFileAttribute(FileAttributes.Directory, ( FileAttributes )attrs))
            {
                return(true);
            }

            throw new UnmatchedFileSystemEntryTypeException(QuickIOFileSystemEntryType.Directory, QuickIOFileSystemEntryType.File, uncPath);
        }
        /// <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 (InternalHelpers.ContainsFileAttribute(PathInfo.Attributes, FileAttributes.Directory))
                {
                    securityInfo = new DirectorySecurity();
                    securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray);
                }
                else
                {
                    securityInfo = new FileSecurity();
                    securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray);
                }

                return(securityInfo);
            }
            finally
            {
                Win32SafeNativeMethods.LocalFree(zeroHandle);
                Win32SafeNativeMethods.LocalFree(pSecurityDescriptor);
            }
        }