简体   繁体   English

如何在Windows 8中设置共享权限

[英]How do I set share permissions in Windows 8

The code below works just fine when setting permissions on NT based machines, but something about windows 8 works differently. 在基于NT的计算机上设置权限时,以下代码可以正常工作,但是Windows 8的工作原理有所不同。 The code will create the share on Windows 8, but will not affect the "Share Permissions" page of the share properties. 该代码将在Windows 8上创建共享,但不会影响共享属性的“共享权限”页面。

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. 就像我说的一样,相同的代码可以在Vista / Win 7中完成此操作,但不能在Windows 8中完成。

Can someone please tell me how to do this in Windows 8? 有人可以告诉我如何在Windows 8中执行此操作吗? The answer can be in VB or C#, either is fine. 答案可以是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

I found the problem. 我发现了问题。

The issue is I messed up the access flag. 问题是我弄乱了访问标志。

In my code its 2302127 在我的代码中,其2302127

It should read 2032127 它应该显示为2032127

The 0 and the 3 were flipped for some reason. 0和3出于某种原因被翻转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置WCF Windows服务的权限? - How do I set permissions for a WCF Windows Service? 如何使我的应用程序请求共享Windows 7上的文件夹的权限? - How do I make my application request permissions to share folders on Windows 7? 从C#控制台应用程序终止Windows进程:如何设置权限? - Killing a Windows process from a C# console application: how do I set permissions? 如何跨一组并行任务共享事务 - How do I share a transaction across a set of parallel tasks 如何设置正确的Windows服务权限 - How to set right Windows Service permissions 如何在Windows上设置SQLiteConnection? - How do I set up a SQLiteConnection on Windows? 如何在Windows 7中设置显示器方向? - How do I set the monitor orientation in Windows 7? 以管理员身份运行,如何检查某些Windows帐户是否有权读取目录? - Running as Admin, How do I check if some windows account has permissions to read a directory? 如何设置 Azure 应用程序的权限,以便应用程序可以访问站点及其驱动器? - How do I set up permissions for an Azure Application so that an application can access a site and its drives? 如何在Windows Phone的“共享给”目标列表中显示我的应用 - How do I show my app in the list of “Share To” targets on Windows Phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM