I had created the windows service and running it with Local System account. This service is reading the user files and finding it's owner. While getting the access of the file to find the owner it is throwing below exception :
Method failed with unexpected error code 3.
StackTrace : at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext) at System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory) at System.Security.AccessControl.FileSecurity..ctor(String fileName, AccessControlSections includeSections)
Below is the Sample C# code I am using to get the Owner of the file.
IdentityReference sid = null;
string owner = null;
FileSecurity fileSecurity = File.GetAccessControl(foundFile);
sid = fileSecurity.GetOwner(typeof(SecurityIdentifier));
NTAccount ntAccount = sid.Translate(typeof(NTAccount)) as NTAccount;
owner = ntAccount.Value;
foundFile contains the file path which was read from the directory. I am gone through below link but it seems to be different than my issue : DirectoryInfo.GetAccessControl method always fails
Kindly Help me as I am facing this issue for bunch of the users and it is working for other users.
I experienced a similar issue from
Directory.GetAccessControl(string)
. My problem was that the folder being passed as parameter did not exist.Your parameter name would suggest that the file has been found, but you might benefit from
File.Exists(string)
before callingFile.GetAccessControl()
. Atleast make sure that the folder/file is there at runtime.