« Fuzzy fonts in MindManager X5Lookahead (or -behind) assertions in regular expressions »

Accessing directory owner information in .NET 2

11/08/05

Permalink 10:14:23 am
Categories: General, Programming, .NET

Accessing directory owner information in .NET 2

I just came upon this question in a newsgroup and had a quick look at the new classes that .NET 2 offers for access to the Windows file system ACLs and general system security information. In short, to get to the name of a user or group that's the owner of a directory in the file system, you can use the following code:

  DirectoryInfo directoryInfo = new DirectoryInfo(@"c:\Windows");
  DirectorySecurity directorySecurity = 
    directoryInfo.GetAccessControl(AccessControlSections.Owner);
  IdentityReference identityReference = 
    directorySecurity.GetOwner(typeof(NTAccount));
  // now access identityReference.Value for the readable name

The one thing here that's a bit hard to find out is the usage of the GetOwner method. Current MS docs tell only that the type you need to pass in is "the primary group for which to get the owner". I needed to use Reflector to find out that the method that's actually being called in the end is SecurityIdentifier.Translate, and that the type in question must be one of those that the SecurityIdentifier.IsValidTargetType method likes. The docs for that method, finally, told me that the only two types currently considered valid are the SecurityIdentifier itself and the System.Security.Principal.NTAccount. Good, works fine :-)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Please complete the song title below. Hint: enter 'h', 'e', 'a', 'v', 'e', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
July 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31