如何在Windows 8操作系统中设置共享权限

本文关键字:设置 共享 权限 操作系统 Windows | 更新日期: 2023-09-27 18:11:30

当在基于NT的机器上设置权限时,下面的代码工作得很好,但是windows 8的工作方式有所不同。该代码将在Windows 8上创建共享,但不会影响共享属性的"共享权限"页面。

要进入属性页面,我说的是右键单击共享并选择属性。从那里选择"共享"选项卡,然后选择"高级共享"。从这里点击"权限"按钮。群组将显示"Everyone",并且在对话框的底部有"Full Control"、"Change"answers"Read"权限选项。这些是我需要通过编程选择的选项。就像我说的,同样的代码可以在Vista/Win 7中实现,但在Windows 8中不行。

谁能告诉我如何在Windows 8中做到这一点?答案可以是VB或c#,两者都可以。
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
    ' For more info see:
    'http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/de213b61-dc7e-4f33-acdb-893aa96837fa/c-set-directory-sharing-permission-full-control-for-everyone-programmatically-in-windows-7-or?forum=windowssdk
    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  ' <-Full Access
    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 ' SE_DACL_PRESENT
    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
Public Enum MethodStatus
    Success = 0     'Success
    AccessDenied = 2    'Access denied
    UnknownFailure = 8  'Unknown failure
    InvalidName = 9     'Invalid name
    InvalidLevel = 10   'Invalid level
    InvalidParameter = 21   'Invalid parameter
    DuplicateShare = 22     'Duplicate share
    RedirectedPath = 23     'Redirected path
    UnknownDevice = 24  'Unknown device or directory
    NetNameNotFound = 25    'Net name not found
End Enum

如何在Windows 8操作系统中设置共享权限

我找到问题了。

问题是我弄乱了访问标志。

在我的代码中是2302127

应该是2032127

0和3由于某种原因被颠倒了