The code below works just fine when setting permissions on NT based machines, but something about windows 8 works differently. The code will create the share on Windows 8, but will not affect the "Share Permissions" page of the share properties.
To get to the properties page I'm talking about right click on a share and choose properties. From there select the "Sharing" tab and choose "Advanced Sharing." From here click the "Permissions" button. The groups will show "Everyone" and there will be options for "Full Control", "Change", and "Read" permissions towards the bottom of the dialog. These are the options I need to programmatically have selected. Like I said, the same code accomplishes this in Vista/Win 7 but not Windows 8.
Can someone please tell me how to do this in Windows 8? The answer can be in VB or C#, either is fine.
Private Function CreateWindowsShare(ByVal DirectoryToShare As String) As String
Dim ManageClass As New ManagementClass("Win32_Share")
Dim ReturnStatus As UInt32 = 0
Dim i As Integer = 1
Dim CreatedShareName As String
Do
CreatedShareName = IIf(i = 1, "TestShare", "TestShare" & i)
Dim inParams As ManagementBaseObject = ManageClass.GetMethodParameters("Create")
inParams("Description") = ""
inParams("Name") = CreatedShareName
inParams("Path") = DirectoryToShare
inParams("Type") = &H0
Dim outParams As ManagementBaseObject = ManageClass.InvokeMethod("Create", inParams, Nothing)
ReturnStatus = Convert.ToUInt32(outParams.Properties("ReturnValue").Value)
i += 1
Loop While ReturnStatus = MethodStatus.DuplicateShare
If ReturnStatus <> 0 Then
Throw New Exception("Unable to create share.")
End If
Dim ntAccount As New NTAccount("Everyone")
Dim UserSID As SecurityIdentifier = ntAccount.Translate(GetType(SecurityIdentifier))
Dim UtenteSIDArray(UserSID.BinaryLength) As Byte
UserSID.GetBinaryForm(UtenteSIDArray, 0)
Dim UserTrustee As New ManagementClass(New ManagementPath("Win32_Trustee"), Nothing)
UserTrustee("Name") = "Everyone"
UserTrustee("SID") = UtenteSIDArray
Dim UserACE As New ManagementClass(New ManagementPath("Win32_Ace"), Nothing)
UserACE("AccessMask") = 2302127
UserACE("AceFlags") = AceFlags.ObjectInherit Or AceFlags.ContainerInherit
UserACE("AceType") = AceType.AccessAllowed
UserACE("Trustee") = UserTrustee
Dim UserSecurityDescriptor As New ManagementClass(New ManagementPath("Win32_SecurityDescriptor"), Nothing)
UserSecurityDescriptor("ControlFlags") = 4
UserSecurityDescriptor("DACL") = New Object() {UserACE}
Dim ShareClass As New ManagementClass("Win32_Share")
Dim Share As New ManagementObject(ShareClass.Path.ToString & ".Name='" & CreatedShareName & "'")
Share.InvokeMethod("SetShareInfo", New Object() {Int32.MaxValue, "", UserSecurityDescriptor})
Return CreatedShareName
End Function
End Class
Public Enum MethodStatus
Success = 0
AccessDenied = 2
UnknownFailure = 8
InvalidName = 9
InvalidLevel = 10
InvalidParameter = 21
DuplicateShare = 22
RedirectedPath = 23
UnknownDevice = 24
NetNameNotFound = 25
End Enum