Getting Permissions for a Windows Share?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am trying to write a program that will iterate through all the shares on a Windows machine and read the permissions each user has on those shares. I have written a little program that uses the
<a title="Win32_Share http://msdn.microsoft.com/en-us/library/aa394435(v=VS.85).aspx" target="_blank
Win32_Share class to iterate through all the shares on a machine and read the permissions
you have on the share, but have not found a way to read the permissions
others have on that share.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; try
{
<span style="color:Green; //get all the shares associated with the computer (will throw exception if you dont have read permissions)
<span style="color:Green; //Where computer name is the name of the computer (asset tag)
ManagementClass manClass = <span style="color:Blue; new ManagementClass(<span style="color:#A31515; @"\" +computerName +<span style="color:#A31515; @"rootcimv2:Win32_Share");

<span style="color:Green; //run through all the shares
<span style="color:Blue; foreach (ManagementObject objShare <span style="color:Blue; in manClass.GetInstances())
{
<span style="color:Green; //ignore system shares
<span style="color:Blue; if (!objShare.Properties[<span style="color:#A31515; "Name"].Value.ToString().Contains(<span style="color:#A31515; $))
{
<span style="color:Green; //print the share name and location
textBox2.Text += String.Format(<span style="color:#A31515; "Share Name: {0} Share Location: {1}", objShare.Properties[<span style="color:#A31515; "Name"].Value, objShare.Properties[<span style="color:#A31515; "Path"].Value) + <span style="color:#A31515; "n";
Int32 permissions = 0;

<span style="color:Blue; try
{
<span style="color:Green; //get the rights you have
ManagementBaseObject result = objShare.InvokeMethod(<span style="color:#A31515; "GetAccessMask", <span style="color:Blue; null, <span style="color:Blue; null);

<span style="color:Green; //value meanings: http://msdn.microsoft.com/en-us/library/aa390438(v=vs.85).aspx
permissions = Convert.ToInt32(result.Properties[<span style="color:#A31515; "ReturnValue"].Value);
}
<span style="color:Blue; catch (ManagementException me)
{
permissions = -1; <span style="color:Green; //no permissions have been set
}

<span style="color:Blue; if(permissions == -1) textBox2.Text += <span style="color:#A31515; "You have permissions: Permissions not set nn"; <span style="color:Blue; else textBox2.Text += <span style="color:#A31515; "You have permissions: " + permissions + <span style="color:#A31515; "nn";

}
}

}
<span style="color:Blue; catch (Exception ee)
{
textBox2.Text = ee.ToString();
}
[/code]
Does anyone know if, and how, this can be accomplished?
Thanks,
Pete

View the full article
 
Back
Top