elsvieta
2009-01-09 20:48:24 UTC
Hi all,
I am using the code below in order to put together a list of folders, their
owner and possibly some other related data (size, last access, etc.):
static void Main(string[] args)
{
string strOwner;
string strOwnerDomain;
string strOwnerSID;
string strPath = "c:\\temp";
ManagementObject mgmt = new
ManagementObject("Win32_LogicalFileSecuritySetting.path='" + strPath + "'");
ManagementBaseObject secDesc = mgmt.InvokeMethod("GetSecurityDescriptor",
null, null);
ManagementBaseObject descriptor = secDesc.Properties["Descriptor"].Value as
ManagementBaseObject;
ManagementBaseObject owner = descriptor.Properties["Owner"].Value as
ManagementBaseObject;
strOwner = owner.Properties["Name"].Value.ToString();
strOwnerDomain = owner.Properties["Domain"].Value.ToString();
strOwnerSID = owner.Properties["SID"].Value.ToString();
}
As long as I am looking at a path in my computer (having admin rights)
things seem to be OK: the code works, and I am told that most of everything
has Administrators as owner and the domain is BUILTIN (don't know exactly
what that is ...).
However, if I change the path to some network path where I can usually list
the contents of a folder, read, write, I get an error as soon as I get to the
line where I invoke GetSecurityDescriptor
System.Management.ManagementStatus.NotFound) and the stack trace is:
System.Management.ManagementException was unhandled
Message="Not found "
Source="System.Management"
StackTrace:
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at System.Management.ManagementObject.InvokeMethod(String methodName,
ManagementBaseObject inParameters, InvokeMethodOptions options)
at fstest2.Program.Main(String[] args) in
C:\Projects\fstest2\fstest2\Program.cs:line 21
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I guess this happens because I miss the necessary credentials - nevertheless
I am able to right click the folder in question and see the Owner (right
click the folder->Properties->Security->Advanced->Owner tab (Warning saying I
cannot change the owner) and click OK - the owner is shown as
S-1-5-21-1672067133-645457607-925700815-7284.
What do I do wrong in the code above that prevents me to see the owner of
the folder? It may not be something that has to do with credentials since I
can see the owner using the OS.
Thanks,
elsvieta
I am using the code below in order to put together a list of folders, their
owner and possibly some other related data (size, last access, etc.):
static void Main(string[] args)
{
string strOwner;
string strOwnerDomain;
string strOwnerSID;
string strPath = "c:\\temp";
ManagementObject mgmt = new
ManagementObject("Win32_LogicalFileSecuritySetting.path='" + strPath + "'");
ManagementBaseObject secDesc = mgmt.InvokeMethod("GetSecurityDescriptor",
null, null);
ManagementBaseObject descriptor = secDesc.Properties["Descriptor"].Value as
ManagementBaseObject;
ManagementBaseObject owner = descriptor.Properties["Owner"].Value as
ManagementBaseObject;
strOwner = owner.Properties["Name"].Value.ToString();
strOwnerDomain = owner.Properties["Domain"].Value.ToString();
strOwnerSID = owner.Properties["SID"].Value.ToString();
}
As long as I am looking at a path in my computer (having admin rights)
things seem to be OK: the code works, and I am told that most of everything
has Administrators as owner and the domain is BUILTIN (don't know exactly
what that is ...).
However, if I change the path to some network path where I can usually list
the contents of a folder, read, write, I get an error as soon as I get to the
line where I invoke GetSecurityDescriptor
System.Management.ManagementStatus.NotFound) and the stack trace is:
System.Management.ManagementException was unhandled
Message="Not found "
Source="System.Management"
StackTrace:
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at System.Management.ManagementObject.InvokeMethod(String methodName,
ManagementBaseObject inParameters, InvokeMethodOptions options)
at fstest2.Program.Main(String[] args) in
C:\Projects\fstest2\fstest2\Program.cs:line 21
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I guess this happens because I miss the necessary credentials - nevertheless
I am able to right click the folder in question and see the Owner (right
click the folder->Properties->Security->Advanced->Owner tab (Warning saying I
cannot change the owner) and click OK - the owner is shown as
S-1-5-21-1672067133-645457607-925700815-7284.
What do I do wrong in the code above that prevents me to see the owner of
the folder? It may not be something that has to do with credentials since I
can see the owner using the OS.
Thanks,
elsvieta