获取文件的安全信息

System.Security.AccessControl 命名空间下,

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string dirLocation = @"C:\Program Files\IIS\Microsoft Web Deploy";
            
//获取文件集合
            
//IEnumerable<FileInfo> new to .NET 4.0
            var fileInfo = new DirectoryInfo(dirLocation).EnumerateFiles();

            
foreach (var file in fileInfo)
            {
                Console.WriteLine(
"----- Access Control List Entries for {0}----------\n",file.Name);
                FileSecurity fileSec 
= file.GetAccessControl();
                
//NTAccount表示一个用户或组帐户
                var authRuleColl = fileSec.GetAccessRules(truetruetypeof(NTAccount));
                
foreach (FileSystemAccessRule fsaRule in authRuleColl)
                {
                    Console.WriteLine(
"IdentityReference:{0}", fsaRule.IdentityReference);
                    Console.WriteLine(
"AccessControlType:{0}", fsaRule.AccessControlType);
                    Console.WriteLine(
"FileSystemRights:{0}", fsaRule.FileSystemRights);
                    Console.WriteLine();
                }
                Console.WriteLine(
"-----------------------------------------------------");

            }
            Console.ReadLine();
        }
    }
}


 

posted @ 2010-06-21 08:56  smodi  阅读(222)  评论(0编辑  收藏  举报