13

フォルダーにアクセス許可を割り当てる必要があり、それは C#.NET を使用してプログラムで子フォルダーとファイルです。私は以下のようにこれをやっています:

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」フォルダーには割り当てません。 ここに画像の説明を入力

FileSystemAccessRuleQ:インスタンスを定義して、ROOT フォルダー、サブフォルダー、およびファイルにアクセス許可を割り当てるにはどうすればよいですか?

4

1 に答える 1

12

PropagationFlags.InheritOnlyフラグを削除するだけです。ターゲット フォルダーに ACE を適用しないように指定することによって。PropagationFlags.None代わりに使用してください。このMSDN の記事が役立つ場合があります。

于 2012-05-14T06:24:15.420 に答える