示例#1
0
        public static FileSystemPrincipal GetOwner(FileSystemInfo item, LookupCache cache)
        {
            SecurityIdentifier  ownerSID       = null;
            IdentityReference   ownerNTAccount = null;
            CachedUserPrincipal ownerPrincipal = null;

            try
            {
                FileSystemSecurity fac = null;

                if (item is FileInfo)
                {
                    fac = File.GetAccessControl(item.FullName);
                }
                if (item is DirectoryInfo)
                {
                    fac = Directory.GetAccessControl(item.FullName);
                }

                if (fac != null)
                {
                    ownerSID = fac.GetOwner(typeof(SecurityIdentifier)) as SecurityIdentifier;

                    if (ownerSID != null)
                    {
                        ownerNTAccount = cache.GetName <NTAccount>(FileSystemObjectTypes.NTAccount, ownerSID);
                        ownerPrincipal = cache.GetName <CachedUserPrincipal>(FileSystemObjectTypes.Principal, ownerSID);
                    }
                }

                return(new FileSystemPrincipal(ownerSID, ownerNTAccount, ownerPrincipal));
            }
            catch (Exception)
            {
            }

            return(null);
        }