Click here to Skip to main content
14,365,424 members

Determining the Owner of a File/Folder in C#.NET and Dealing with General Exceptions

Rate this:
4.90 (18 votes)
Please Sign up or sign in to vote.
4.90 (18 votes)
18 Jun 2015CPOL
This article will help you determining the owner of a file/folder which is displayed in the Advanced Security Settings.

Determining the Owner of a File/Folder in C#.NET

In order to determine the owner of a file/folder, you would need to use the classes from the following namespaces:

using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

Put the following lines in a function and it will give you the owner name:

FileSecurity fileSecurity = File.GetAccessControl(path);
IdentityReference sid = fileSecurity.GetOwner(typeof(SecurityIdentifier));
NTAccount ntAccount = sid.Translate(typeof(NTAccount)) as NTAccount;
string owner = ntAccount.Value;

Here ‘path’ is the location of the file/folder. 

General Exceptions

  1. UnauthorizedAccessException: If you don’t have access to the file, you will get this exception.
  2. IdentityNotMappedException: "Some or all Identity References could not be transalated."

Whenever any user is created on the machine, a unique security ID is assigned. This SID can be converted to Domain Name\Username format by translating the SID to NTAccount type, but if that user is removed from the machine, then only SID exists. Now if you try to convert the SID to NTAccount type, the above exception would occur. In that case, you may just want to display the SID as follows: 

IdentityReference sid = null;
string owner = null;
try
{

FileSecurity fileSecurity = File.GetAccessControl(path);
sid = fileSecurity.GetOwner(typeof(SecurityIdentifier));
NTAccount ntAccount = sid.Translate(typeof(NTAccount)) as NTAccount;
owner = ntAccount.Value;

}
catch (IdentityNotMappedException ex)
{

If (sid != null)
owner = sid.ToString();

}

Optimizing the Code If You Want to Display the Owner Name of Large Number of Files

If you have too many files, getting the SIDs and translating it to DomainName\Username(s) would be very time consuming. You can optimize the code by finding the SIDs for all the files and once you have all the SIDs, translate them to DomainName\Username.

For example: If you have 10000 files, determine the SID for all the 10000 files. Let's say you get 7 SIDs. Now translate only those 7 SIDs to DomainName\Username format. It will definitely speed up the process.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Komal Mangal
Software Developer
India India
For more technical articles please visit my blog at-
http://komalmangal.blogspot.in

Comments and Discussions

 
QuestionGood Pin
Dominic Abraham12-Jul-15 19:22
memberDominic Abraham12-Jul-15 19:22 
AnswerRe: Good Pin
Komal Mangal12-Jul-15 19:56
memberKomal Mangal12-Jul-15 19:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 18 Jun 2015

Tagged as

Stats

18.9K views
18 bookmarked