2011-09-17 61 views

回答

8

你应该可以使用 System.Security.AccessControl命名空间。

System.Security.AccessControl; 


public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) 
    { 
    // Create a new DirectoryInfo object. 
    DirectoryInfo dInfo = new DirectoryInfo(FileName); 


    // Get a DirectorySecurity object that represents the 
    // current security settings. 
    DirectorySecurity dSecurity = dInfo.GetAccessControl(); 


    // Add the FileSystemAccessRule to the security settings. 
    dSecurity.AddAccessRule(new FileSystemAccessRule(Account, 
    Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, 
    ControlType)); 


    // Set the new access settings. 
    dInfo.SetAccessControl(dSecurity); 


} 

调用示例:

//Get current user 
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
//Deny writing to the file 
AddDirectorySecurity(@"C:\Users\Phil\Desktop\hello.ini",user, FileSystemRights.Write, AccessControlType.Deny); 
+1

那实际上可以工作! –

+1

非常欢迎 – ApolloSoftware

+2

再次感谢AmitApollo! –