2012-05-14 17 views
12

C#.NETを使用してフォルダとそのプログラムの子フォルダとファイルにアクセス許可を割り当てる必要があります。私はこれを次のようにしています:'このフォルダ、サブフォルダおよびファイル'にアクセス制御リスト(ACL)のアクセス許可をプログラムで割り当てます。

var rootDic = @"C:\ROOT"; 
var identity = "NETWORK SERVICE"; //The name of a user account. 
try 
{ 
    var accessRule = new FileSystemAccessRule(identity, 
         fileSystemRights: FileSystemRights.Modify, 
         inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
         propagationFlags: PropagationFlags.InheritOnly, 
         type: AccessControlType.Allow); 

    var directoryInfo = new DirectoryInfo(rootDic); 

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

    // Add the FileSystemAccessRule to the security settings. 
    dSecurity.AddAccessRule(accessRule); 

    // Set the new access settings. 
    directoryInfo.SetAccessControl(dSecurity); 
} 
catch (Exception ex) 
{ 
    //... 
} 

私の 'C:\ ROOT'フォルダには、パーミッションが割り当てられています。しかし、それはサブフォルダーとファイルのみに権限を割り当てますが、 'ROOT'フォルダーは割り当てません。 enter image description here

Q:どのように私は、サブフォルダとファイル、ROOTフォルダへのアクセス許可を割り当てるためにFileSystemAccessRuleインスタンスを定義することができますか?

答えて

11

フラグPropagationFlags.InheritOnlyを削除するだけで済みます。 ACEがターゲットフォルダに適用されるべきではないことを指定します。代わりにPropagationFlags.Noneを使用してください。 MSDN articleが参考になる場合があります。

+0

@Kibria私は好奇心が強いです。これは助けになりましたか? –

+1

はい、ありがとう! – Kibria

関連する問題