2017-05-09 13 views
0

現在のユーザーに対してListフォルダのコンテンツのアクセス許可を持っていますが、下のコードのために。フォルダにc#での読み取りと実行のアクセス許可があるかどうかを確認する必要があります。ユーザーがコンテンツの一覧のアクセス許可のみを持っている場合は失敗します。

 WindowsIdentity currentuser = WindowsIdentity.GetCurrent(); 
     var domainAndUser = currentuser.Name; 
     DirectoryInfo dirInfo = new DirectoryInfo(downloadSource.BasePath); 
     DirectorySecurity dirAC = dirInfo.GetAccessControl(AccessControlSections.All); 
     AuthorizationRuleCollection rules = dirAC.GetAccessRules(true, true, typeof(NTAccount)); 
     foreach(AuthorizationRule rule in rules) 
     { 
      if (rule.IdentityReference.Value.Equals(domainAndUser, StringComparison.CurrentCultureIgnoreCase)) 
       { 
         if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.ReadAndExecute) > 0) 
         return true; 
       } 
     } 
     return false; 
+0

代替は、列挙型のフラグチェックを使用します。私もDirectory.GetFiles(パス)を使用してみましたが、これはListコンテンツのパーミッションで実行されます。これを解決するには? –

答えて

0

はあなたが旗

read = 1 
execute = 2 
ReadAndExecute = 3 

を持っているし、その後

((FileSystemRights & ReadAndExecute) > 0) is true 

FileSystemRights = 1 (read only, no execute) 

を引き起こすと仮定し、単純なビット論理エラーしているように見えます

ので、代わりに次のチェックをしてみてください:

if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.ReadAndExecute) == FileSystemRights.ReadAndExecute) 

それはReadAndExecuteからではない任意のしかしすべてフラグが必要とされていることを確認します。私はそれはまた、ユーザーが書き込みアクセス権を持っていないような場所で失敗したGetAccessControlを使用する場合は

if ((((FileSystemAccessRule)rule).FileSystemRights).HasFlag(FileSystemRights.ReadAndExecute)) 
+0

それは素晴らしい仕事をしています...たくさんありがとう –

関連する問題