2010-03-01 49 views
0

我想使用ACL拒絕/只讀訪問可移動驅動器。拒絕/只讀訪問可移動驅動器

我搜索了一下,發現我可以使用setsecurityinfo來完成。

我提到MSDN和pinvoke.net,但沒能成功設置

幫助C#或Vb.net需要

回答

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); 

    } 

你應該根據這個建立你的具體解決方案。

請記住,您需要成爲所有者或擁有「更改權限」才能進行這些更改。

+0

謝謝但上面的例子只適用於文件而不適用於卷 – bts 2010-03-01 12:10:49

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

私人靜態外部UINT SetNamedSecurityInfoW(字符串pObjectName,SE_OBJECT_TYPE對象類型,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 =真)] 私有靜態的extern BOOL ConvertStringSidToSid(字符串StringSid,參考文獻的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 }

[F滯後]私人枚舉SECURITY_INFORMATION:UINT { 所有者= 00000001, 組= 0x00000002, DACL = 0x00000004, 的SacI = 0x00000008, ProtectedDacl = 0x80000000的, ProtectedSacl = 0x40000000之後, UnprotectedDacl = 0x20000000, UnprotectedSacl = 0x10000000的 }

其中k:是可移動驅動器