拒绝/只读访问可移动驱动器

时间:2010-03-01 10:21:54

标签: .net

我想使用ACL拒绝/只读访问可移动驱动器。

我搜索并发现我可以使用setsecurityinfo完成。

我提到了Msdn和pinvoke.net,但无法成功设置

需要c#或Vb.net帮助

2 个答案:

答案 0 :(得分:0)

MSDN对FileSystemAccessRule类有以下示例:

   // Removes an ACL entry on the specified file for the specified account.
    public static void RemoveFileSecurity(string fileName, string account,
        FileSystemRights rights, AccessControlType controlType)
    {

        // Get a FileSecurity object that represents the
        // current security settings.
        FileSecurity fSecurity = File.GetAccessControl(fileName);

        // Remove the FileSystemAccessRule from the security settings.
        fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
            rights, controlType));

        // Set the new access settings.
        File.SetAccessControl(fileName, fSecurity);

    }

您应该基于此构建您的特定解决方案。

请记住,您需要成为所有者或拥有“更改权限”才能进行这些更改。

答案 1 :(得分:0)

    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]

private static extern uint SetNamedSecurityInfoW(String pObjectName,SE_OBJECT_TYPE ObjectType,SECURITY_INFORMATION SecurityInfo,IntPtr psidOwner,IntPtr psidGroup,IntPtr pDacl,IntPtr pSacl);

    private void Form1_Load(object sender, EventArgs e)
    {
     SetNamedSecurityInfo ("\\\\.\\K:",SE_OBJECT_TYPE.SE_FILE_OBJECT,SECURITY_INFORMATION.ProtectedDacl, null, null, null, null);

    }

    private void SetNamedSecurityInfo(string p, object SE_FILE_OBJECT, object DACL_SECURITY_INFORMATION, object NULL, object NULL_5, object paclNew, object NULL_7)
    {
        throw new Exception("The method or operation is not implemented.");
    }

[DllImport(“Advapi32.dll”,SetLastError = true)] private static extern bool ConvertStringSidToSid(String StringSid,ref IntPtr Sid);

私人枚举SE_OBJECT_TYPE {     SE_UNKNOWN_OBJECT_TYPE = 0,
    SE_FILE_OBJECT,     SE_SERVICE,     SE_PRINTER,     SE_REGISTRY_KEY,     SE_LMSHARE,     SE_KERNEL_OBJECT,     SE_WINDOW_OBJECT,     SE_DS_OBJECT,     SE_DS_OBJECT_ALL,     SE_PROVIDER_DEFINED_OBJECT,     SE_WMIGUID_OBJECT,SE_REGISTRY_WOW64_32KEY }

[Flags] private enum SECURITY_INFORMATION:uint {     所有者= 0x00000001,     组= 0x00000002,     Dacl = 0x00000004,     Sacl = 0x00000008,     ProtectedDacl = 0x80000000,     ProtectedSacl = 0x40000000,     UnprotectedDacl = 0x20000000,     UnprotectedSacl = 0x10000000 }

其中k:是可移动驱动器