<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>You had me at "Hello World" : WMI</title><link>http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx</link><description>Tags: WMI</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to Give Authenticated Users or Everyone Access to Your Share Programmatically</title><link>http://blogs.msdn.com/helloworld/archive/2009/07/13/how-to-give-authenticated-users-or-everyone-access-to-your-share-programmatically.aspx</link><pubDate>Mon, 13 Jul 2009 19:41:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9831920</guid><dc:creator>HelloWorld</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/9831920.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=9831920</wfw:commentRss><description>&lt;p&gt;Another follow-up from my previous article, &lt;a href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx"&gt;Programmatically Configuring Permissions on a Share&lt;/a&gt;, David B asked a question, how to share a folder to Everyone, instead of to a specific users. This article will answer that question, based on the code on my previous article.&lt;/p&gt;  &lt;p&gt;That is an interesting question, since ‘Everyone’ can be replaced with ‘Authenticated Users’, ‘Network Service’, etc.&lt;/p&gt;  &lt;p&gt;First, if you need only to give Everyone read-only access permission, the easiest thing is to set the DACL property of Win32_SecurityDescriptor to null. This is not equal with an array of null. An array of null will lock everyone out from this share.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ManagementObject &lt;/span&gt;secDescriptor = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementClass&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementPath&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Win32_SecurityDescriptor&amp;quot;&lt;/span&gt;), &lt;span style="color: blue"&gt;null&lt;/span&gt;);
secDescriptor[&lt;span style="color: #a31515"&gt;&amp;quot;ControlFlags&amp;quot;&lt;/span&gt;] = 4; &lt;span style="color: green"&gt;//SE_DACL_PRESENT 
&lt;/span&gt;secDescriptor[&lt;span style="color: #a31515"&gt;&amp;quot;DACL&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you need to be more explicit, or you need to assign other security principal different access, that method above will not work. As soon as you assign someone access to the share, ‘Everyone’ will lose its read access.&lt;/p&gt;

&lt;p&gt;To assign the permission explicitly, the key is to form the correct Win32_Trustee to represent that special account (Network Service, Everyone, Authenticated Users, etc.). Take a look at &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.principal.genericprincipal.aspx"&gt;System.Security.Principal.WellKnownSidType&lt;/a&gt; enum. It has a number of well known sid that you might be interested with.&lt;/p&gt;

&lt;p&gt;What needs to be done is to assign the SID property of the Win32_Trustee object with the security identifier derived from the well known sid.&lt;/p&gt;

&lt;p&gt;Let assume you have this method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private byte&lt;/span&gt;[] GetWellKnwonSid(&lt;span style="color: #2b91af"&gt;WellKnownSidType &lt;/span&gt;SidType)
{
    &lt;span style="color: #2b91af"&gt;SecurityIdentifier &lt;/span&gt;Result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecurityIdentifier&lt;/span&gt;(SidType, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
    &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] sidArray = &lt;span style="color: blue"&gt;new byte&lt;/span&gt;[Result.BinaryLength];
    Result.GetBinaryForm(sidArray, 0);

    &lt;span style="color: blue"&gt;return &lt;/span&gt;sidArray;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Then when Win32_Trustee object is created, assign the SID property as follow:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ManagementObject &lt;/span&gt;Trustee = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementClass&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementPath&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Win32_Trustee&amp;quot;&lt;/span&gt;), &lt;span style="color: blue"&gt;null&lt;/span&gt;);
Trustee[&lt;span style="color: #a31515"&gt;&amp;quot;SID&amp;quot;&lt;/span&gt;] = GetWellKnwonSid(&lt;span style="color: #2b91af"&gt;WellKnownSidType&lt;/span&gt;.WorldSid); &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That code above will create Win32_Trustee for ‘Everyone’. Use this Win32_Trustee to form the Win32_Ace, and you now explicitly assign ‘Everyone’ access to your share.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9831920" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx">WMI</category></item><item><title>Editing Share Permission</title><link>http://blogs.msdn.com/helloworld/archive/2008/07/22/editing-share-permission.aspx</link><pubDate>Tue, 22 Jul 2008 04:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8762733</guid><dc:creator>HelloWorld</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/8762733.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=8762733</wfw:commentRss><description>&lt;P&gt;In my previous post, I have shown you &lt;A href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx" mce_href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx"&gt;how to set up permission on a share&lt;/A&gt;. The thing with Win32_Share, when you set the permission, you basically overwrites the existing permission.&lt;/P&gt;
&lt;P&gt;If you want to edit permission on the share (grant a new user access to the share, or revoke an existing user's permission), then you have to get the security descriptor for that share, and modify it, and then call &lt;A href="http://msdn.microsoft.com/en-us/library/aa393598(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa393598(VS.85).aspx"&gt;Win32_Share.SetShareInfo&lt;/A&gt; to set the share permission.&lt;/P&gt;
&lt;P&gt;To get security descriptor of a share, you can use &lt;A href="http://msdn.microsoft.com/en-us/library/aa394188.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394188.aspx"&gt;Win32_LogicalShareSecuritySetting&lt;/A&gt; class. Then update the security descriptor and set that security descriptor back to the share.&lt;/P&gt;
&lt;P&gt;When calling ManagementObject.GetSecurityDescriptor, it will return a ManagementBaseObject instance, it has two properties, ReturnValue and Descriptor. ReturnValue is an integer value, that tells you whether the operation is successful or not. Look for the possible value &lt;A href="http://msdn.microsoft.com/en-us/library/aa390773(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa390773(VS.85).aspx"&gt;here&lt;/A&gt;. The Descriptor property is an instance of SecurityDescriptor.&lt;/P&gt;
&lt;P&gt;To summarize (for those who love bullet points):&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Get the Win32_Ace instance for the new user.&lt;/LI&gt;
&lt;LI&gt;Get the current security descriptor.&lt;/LI&gt;
&lt;LI&gt;Get the DACL (Array of Win32_Ace) from the security descriptor.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Add&lt;/STRONG&gt; the Win32_Ace for the new user into the Win32_Ace array.&lt;/LI&gt;
&lt;LI&gt;Reassign the edited DACL back to the security descriptor.&lt;/LI&gt;
&lt;LI&gt;Call Win32_Share.SetShareInfo to set the permission.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;You can delete a particular user, or changing the existing permission, by modifying the DACL or SACL in the Security Descriptor.&lt;/P&gt;
&lt;P&gt;This snippet below is just an example on how to read, modify and assign permission on a share, this code was derived from the example on my previous &lt;A href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx" mce_href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx"&gt;post&lt;/A&gt;.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: green"&gt;//Create a new Win32_Ace instance. Please refer to my previous post about creating Win32_Ace.
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NTAccount &lt;/SPAN&gt;account = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NTAccount&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"contoso"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"janedoe"&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier &lt;/SPAN&gt;sid = (&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier&lt;/SPAN&gt;)account.Translate(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier&lt;/SPAN&gt;));
&lt;SPAN style="COLOR: blue"&gt;byte&lt;/SPAN&gt;[] sidArray = &lt;SPAN style="COLOR: blue"&gt;new byte&lt;/SPAN&gt;[sid.BinaryLength];
sid.GetBinaryForm(sidArray, 0);

&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;Trustee = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementClass&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementPath&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Win32_Trustee"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);
Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"Domain"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: #a31515"&gt;"contoso"&lt;/SPAN&gt;;
Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;]   = &lt;SPAN style="COLOR: #a31515"&gt;"janedoe"&lt;/SPAN&gt;;
Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"SID"&lt;/SPAN&gt;]   = sidArray; 

&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;ACE = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementClass&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementPath&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Win32_Ace"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;); 
ACE[&lt;SPAN style="COLOR: #a31515"&gt;"AccessMask"&lt;/SPAN&gt;] = 2032127; 
ACE[&lt;SPAN style="COLOR: #a31515"&gt;"AceFlags"&lt;/SPAN&gt;]   = 3; 
ACE[&lt;SPAN style="COLOR: #a31515"&gt;"AceType"&lt;/SPAN&gt;]    = 0; 
ACE[&lt;SPAN style="COLOR: #a31515"&gt;"Trustee"&lt;/SPAN&gt;]    = Trustee; 

&lt;SPAN style="COLOR: green"&gt;//After we have the new Win_32Ace, now we need to get the existing Ace instances (DACL).
//Create an instance of Win32_LogicalSecuritySetting, set the path to the server and the share.
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;Win32LogicalSecuritySetting = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;@"\\ContosoServer\root\cimv2:Win32_LogicalShareSecuritySetting.Name='JohnShare'"&lt;/SPAN&gt;);

&lt;SPAN style="COLOR: green"&gt;//Call the GetSecurityDescriptor method. This method returns one out parameter.
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject &lt;/SPAN&gt;Return = Win32LogicalSecuritySetting.InvokeMethod(&lt;SPAN style="COLOR: #a31515"&gt;"GetSecurityDescriptor"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);
    
&lt;SPAN style="COLOR: green"&gt;//The return value of that call above has two properties, ReturnValue, which you can use
//to read the status of the call (failed, success, etc.), and Descriptor, which is an instance
//of Win32_SecurityDescriptor.
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Int32 &lt;/SPAN&gt;ReturnValue = &lt;SPAN style="COLOR: #2b91af"&gt;Convert&lt;/SPAN&gt;.ToInt32(Return.Properties[&lt;SPAN style="COLOR: #a31515"&gt;"ReturnValue"&lt;/SPAN&gt;].Value);

&lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(ReturnValue != 0)
    &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Exception&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Error when calling GetSecurityDescriptor. Error code : {0}."&lt;/SPAN&gt;, ReturnValue));

&lt;SPAN style="COLOR: green"&gt;//Retrieve the array of DACL from the Security Descriptor.
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject &lt;/SPAN&gt;SecurityDescriptor = Return.Properties[&lt;SPAN style="COLOR: #a31515"&gt;"Descriptor"&lt;/SPAN&gt;].Value &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject&lt;/SPAN&gt;;
&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject&lt;/SPAN&gt;[] DACL = SecurityDescriptor[&lt;SPAN style="COLOR: #a31515"&gt;"DACL"&lt;/SPAN&gt;] &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject&lt;/SPAN&gt;[];

&lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(DACL == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
    DACL = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementBaseObject&lt;/SPAN&gt;[] { ACE };
&lt;SPAN style="COLOR: blue"&gt;else
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;Array&lt;/SPAN&gt;.Resize(&lt;SPAN style="COLOR: blue"&gt;ref &lt;/SPAN&gt;DACL, DACL.Length + 1);
    DACL[DACL.Length - 1] = ACE;
}

&lt;SPAN style="COLOR: green"&gt;//Reassign the new DACL array with the new user Ace back to the Win32_SecurityDescriptor instance, and call the
//SetSecurityDescriptor method.
&lt;/SPAN&gt;SecurityDescriptor[&lt;SPAN style="COLOR: #a31515"&gt;"DACL"&lt;/SPAN&gt;] = DACL;&lt;SPAN style="COLOR: green"&gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;Share = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;@"\\ContosoServer\root\cimv2:Win32_Share.Name='JohnShare'"&lt;/SPAN&gt;);
ReturnValue = &lt;SPAN style="COLOR: #2b91af"&gt;Convert&lt;/SPAN&gt;.ToInt32(Share.InvokeMethod(&lt;SPAN style="COLOR: #a31515"&gt;"SetShareInfo"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new object&lt;/SPAN&gt;[] {&lt;SPAN style="COLOR: #2b91af"&gt;Int32&lt;/SPAN&gt;.MaxValue, &lt;SPAN style="COLOR: #a31515"&gt;"This is John's share"&lt;/SPAN&gt;, SecurityDescriptor})); 

&lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(ReturnValue != 0)
    &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Exception&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #a31515"&gt;"Error when calling GetSecurityDescriptor. Error code : {0}."&lt;/SPAN&gt;, ReturnValue));&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8762733" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx">WMI</category></item><item><title>Common AccessMask value when Configuring Share Permission Programmatically</title><link>http://blogs.msdn.com/helloworld/archive/2008/06/10/common-accessmask-value-when-configuring-share-permission-programmatically.aspx</link><pubDate>Tue, 10 Jun 2008 02:23:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8587817</guid><dc:creator>HelloWorld</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/8587817.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=8587817</wfw:commentRss><description>&lt;p&gt;In my previous &lt;a href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx"&gt;post&lt;/a&gt;, I have shown you how to modify share permission using .Net framework. Access Mask is quite granular, most likely you will need to assign a particular user as 'Full Control', 'Change', or 'Read'. In Vista or Server 2008, it will be 'Co-Owner', 'Contributor', or 'Reader'.&lt;/p&gt;  &lt;p&gt;The literal values for those permissions are:&lt;/p&gt;  &lt;p&gt;Full Control/Owner/Co-owner = 2032127 &lt;/p&gt;  &lt;p&gt;Read/Reader = 1179817 &lt;/p&gt;  &lt;p&gt;Change/Contributor = 1179817   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;I created an enum flag like this:&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Flags&lt;/span&gt;]
&lt;span style="color: blue"&gt;public enum &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AccessMaskEnum
&lt;/span&gt;{
    FILE_READ_DATA        = 0x000001,
    FILE_LIST_DIRECTORY   = 0x000001,
    FILE_WRITE_DATA       = 0x000002,
    FILE_ADD_FILE         = 0x000002,
    FILE_APPEND_DATA      = 0x000004,
    FILE_ADD_SUBDIRECTORY = 0x000004,
    FILE_READ_EA          = 0x000008,
    FILE_WRITE_EA         = 0x000010,
    FILE_EXECUTE          = 0x000020,
    FILE_TRAVERSE         = 0x000020,
    FILE_DELETE_CHILD     = 0x000040,
    FILE_READ_ATTRIBUTES  = 0x000080,
    FILE_WRITE_ATTRIBUTES = 0x000100,
    DELETE                = 0x010000,
    READ_CONTROL          = 0x020000,
    WRITE_DAC             = 0x040000,
    WRITE_OWNER           = 0x080000,
    SYNCHRONIZE           = 0x100000,
    OWNER                 = FILE_READ_DATA | FILE_LIST_DIRECTORY | FILE_WRITE_DATA |
                            FILE_ADD_FILE  | FILE_APPEND_DATA    | FILE_ADD_SUBDIRECTORY |
                            FILE_READ_EA   | FILE_WRITE_EA       | FILE_EXECUTE |
                            FILE_TRAVERSE  | FILE_DELETE_CHILD   | FILE_READ_ATTRIBUTES |
                            FILE_WRITE_ATTRIBUTES | DELETE       | READ_CONTROL | 
                            WRITE_DAC      | WRITE_OWNER         | SYNCHRONIZE,
    READ_ONLY             = FILE_READ_DATA | FILE_LIST_DIRECTORY | FILE_READ_EA |
                            FILE_EXECUTE   | FILE_TRAVERSE | FILE_READ_ATTRIBUTES |
                            READ_CONTROL   | SYNCHRONIZE, 
    CONTRIBUTOR           = OWNER &amp;amp; ~(FILE_DELETE_CHILD | WRITE_DAC | WRITE_OWNER)
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;You can assign this enum to the AccessMask property of Win32_Ace instance. For reference, take a look at this &lt;a href="http://msdn.microsoft.com/en-us/library/aa390438(VS.85).aspx"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I got those values by changing the permission using Windows Explorer and then reading the AccessMask, standard disclaimer apply, use it at your own risk. :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8587817" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx">WMI</category></item><item><title>Programmatically Configuring Permissions on a Share</title><link>http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx</link><pubDate>Fri, 06 Jun 2008 20:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8578540</guid><dc:creator>HelloWorld</dc:creator><slash:comments>27</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/8578540.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=8578540</wfw:commentRss><description>&lt;P&gt;I was asked this problem on how to set up permission for a share programmatically using .Net Framework. Well, I am not aware of any API that can do that. Searching does not return any good result.&amp;nbsp;There are&amp;nbsp;lot of resources on how to configure permission settings for&amp;nbsp;local&amp;nbsp;folder, but&amp;nbsp;not so much for UNC path.&amp;nbsp;At the end, I dug msdn and had my solution, using WMI.&lt;/P&gt;
&lt;P&gt;To setup a share, you need these information, the share that you want to setup (securable or trustee), whom you will give the permissions to the share (principal), what kind of permissions you want to give. &lt;/P&gt;
&lt;P&gt;Using this scenario, you have a share &lt;A href="file://contososerver/JohnShare" mce_href="file://\\ContosoServer\JohnShare"&gt;\\ContosoServer\JohnShare&lt;/A&gt;, and you want John Doe (contoso\johndoe) to have full access to this share. The steps to configure the share permissions are as follow:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Create a WMI instance of the principal (Win32_Trustee). &lt;BR&gt;&lt;SPAN style="COLOR: green"&gt;//Getting the Sid value is not required for Vista.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt; &lt;BR&gt;NTAccount &lt;/SPAN&gt;account = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NTAccount&lt;/SPAN&gt;(Domain, UserName); &lt;BR&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier&lt;/SPAN&gt;sid = (&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier&lt;/SPAN&gt;)account.Translate(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;SecurityIdentifier&lt;/SPAN&gt;)); &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;byte&lt;/SPAN&gt;[] sidArray = &lt;SPAN style="COLOR: blue"&gt;new byte&lt;/SPAN&gt;[sid.BinaryLength]; &lt;BR&gt;sid.GetBinaryForm(sidArray, 0); &lt;BR&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;BR&gt;ManagementObject &lt;/SPAN&gt;Trustee = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementClass&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementPath&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Win32_Trustee"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;); &lt;BR&gt;Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"Domain"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: #a31515"&gt;"contoso"&lt;/SPAN&gt;; &lt;BR&gt;Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;]&amp;nbsp;&amp;nbsp; = &lt;SPAN style="COLOR: #a31515"&gt;"johndoe"&lt;/SPAN&gt;;&lt;/FONT&gt; &lt;BR&gt;&lt;FONT face="Courier New"&gt;Trustee[&lt;SPAN style="COLOR: #a31515"&gt;"SID"&lt;/SPAN&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp; = sidArray;&lt;/FONT&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Create a WMI instance of Win32_Ace, assign the Trustee to this Win32_Ace instance. &lt;BR&gt;&lt;FONT color=#333333&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;AdminACE = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementClass&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementPath&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Win32_Ace"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;); &lt;BR&gt;AdminACE[&lt;SPAN style="COLOR: #a31515"&gt;"AccessMask"&lt;/SPAN&gt;] = 2032127; &lt;BR&gt;AdminACE[&lt;SPAN style="COLOR: #a31515"&gt;"AceFlags"&lt;/SPAN&gt;]&amp;nbsp;&amp;nbsp; = 3; &lt;BR&gt;AdminACE[&lt;SPAN style="COLOR: #a31515"&gt;"AceType"&lt;/SPAN&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp; = 0; &lt;BR&gt;AdminACE[&lt;SPAN style="COLOR: #a31515"&gt;"Trustee"&lt;/SPAN&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp; = Trustee; &lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=verda&gt;&lt;BR&gt;To know what values you need to put there, check msdn (&lt;A href="http://msdn.microsoft.com/en-us/library/aa394063(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394063(VS.85).aspx"&gt;link&lt;/A&gt;). I actually encourage you to write an enum flag to encapsulate those values. &lt;BR&gt;In nut shell, 2032127 is for full access, Access Flags 3 is for non-container and container child objects to inherit the ACE, and Ace Type 0 is to allow the trustee to access it. &lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Create a WMI instance of the security descriptor (Win32_SecurityDescriptor) &lt;BR&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;secDescriptor = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementClass&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementPath&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Win32_SecurityDescriptor"&lt;/SPAN&gt;), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;); &lt;BR&gt;secDescriptor[&lt;SPAN style="COLOR: #a31515"&gt;"ControlFlags"&lt;/SPAN&gt;] = 4; &lt;SPAN style="COLOR: green"&gt;//SE_DACL_PRESENT &lt;BR&gt;&lt;/SPAN&gt;secDescriptor[&lt;SPAN style="COLOR: #a31515"&gt;"DACL"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: blue"&gt;new object&lt;/SPAN&gt;[] { AdminACE}; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Now, create a WMI instance of Win32_Share, and setup the security. &lt;BR&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject &lt;/SPAN&gt;share = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ManagementObject&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;@"\\ContosoServer\root\cimv2:Win32_Share.Name='JohnShare'"&lt;/SPAN&gt;); &lt;BR&gt;share.InvokeMethod(&lt;SPAN style="COLOR: #a31515"&gt;"SetShareInfo"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new object&lt;/SPAN&gt;[] {&lt;SPAN style="COLOR: #2b91af"&gt;Int32&lt;/SPAN&gt;.MaxValue, &lt;SPAN style="COLOR: #a31515"&gt;"This is John's share"&lt;/SPAN&gt;, secDescriptor}); &lt;BR&gt;Check the return value of the Invoke, the method returns an Object, convert it to Int32. &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;That code will overwrite the existing permission, so be careful. WMI stuff are available in System.Management assemblies.&lt;/P&gt;
&lt;P&gt;For references, these are the links that you will be interested with, &lt;A href="http://msdn.microsoft.com/en-us/library/aa394501(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394501(VS.85).aspx"&gt;Win32_Trustee&lt;/A&gt;, &lt;A href="http://msdn.microsoft.com/en-us/library/aa394063(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394063(VS.85).aspx"&gt;Win32_ACE&lt;/A&gt;, &lt;A href="http://msdn.microsoft.com/en-us/library/aa394402(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394402(VS.85).aspx"&gt;Win32_SecurityDescriptor&lt;/A&gt;, and &lt;A href="http://msdn.microsoft.com/en-us/library/aa394435.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa394435.aspx"&gt;Win32_Share&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Update (6/9/2008)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;I updated the first step with the code to assign Sid, thanks to &lt;STRONG&gt;David Smith&lt;/STRONG&gt; for his email. With Windows Vista, you do not need to supply Sid. You can supply just the domain name and the user name, it will work. Using Server 2003, and most likely XP, you have to supply all three, user name, domain, and Sid.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8578540" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx">WMI</category></item></channel></rss>