8中设置共享权限

8中设置共享权限

本文介绍了如何在Windows 8中设置共享权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



要访问属性页面我是谈论右键单击共享并选择属性。从那里选择共享选项卡,然后选择高级共享。从这里单击权限按钮。这些组将显示Everyone,并且将在对话框底部显示完全控制,更改和读取权限。这些是我需要以编程方式选择的选项。就像我说的,相同的代码在Vista / Win 7中完成,但不是Windows 8.



有人可以告诉我如何在Windows 8中执行此操作吗?答案可以是VB或C#,也可以。



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

        ' 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

End Class

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中设置共享权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:40