我如何检查子目录和/或文件是否拒绝访问,如果访问被拒绝,我可以进行转换?

时间:2015-12-18 14:21:10

标签: c# .net winforms

我想搜索并传递子目录中也被拒绝访问的目录或文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace Search_Text_In_Files
{
    public partial class Form1 : Form
    {
        StreamWriter w = new StreamWriter(@"e:\textresults.txt");

        public Form1()
        {
            InitializeComponent();

            FindLines(@"E:\", "Green");
        }

        public List<string> FindLines(string DirName, string TextToSearch)
        {
            int counter = 0;
            List<string> findLines = new List<string>();
            DirectoryInfo di = new DirectoryInfo(DirName);
            if (di != null && di.Exists)
            {
                if (CheckFileForAccess(DirName) == true)
                {
                    foreach (string fi in Directory.GetFileSystemEntries(DirName,"*", SearchOption.AllDirectories))
                    {
                        if (string.Compare(fi.Extension, ".cs", true) == 0)
                        //|| string.Compare(fi.Extension, ".txt", true) == 0
                        //|| string.Compare(fi.Extension, ".text", true) == 0)
                        {
                            using (StreamReader sr = fi.OpenText())
                            {
                                string s = "";
                                while ((s = sr.ReadLine()) != null)
                                {
                                    if (s.Contains(TextToSearch))
                                    {
                                        counter++;
                                        findLines.Add(s);
                                        listView1.Items.Add(fi.Name);
                                        w.WriteLine("File Name: " + fi.Name);
                                        w.WriteLine("File Name Line: " + s);
                                        w.WriteLine(" ");
                                    }
                                }
                            }
                        }
                    }
                }
                w.Close();
            }
            return findLines;
        }

        private bool CheckForAccess(string PathName)
        {
            // Determine if the path is a file or a directory

            if (File.Exists(PathName) == true)
                return CheckFileForAccess(PathName);

            if (Directory.Exists(PathName) == true)
                return CheckFolderForAccess(PathName);

            return false;
        }


        private bool CheckFileForAccess(string FileName)
        {
            FileSecurity fs = new FileSecurity(FileName, AccessControlSections.Access);
            if (fs == null)
                return false;

            AuthorizationRuleCollection TheseRules = fs.GetAccessRules(true, true, typeof(NTAccount));
            if (TheseRules == null)
                return false;

            return CheckACL(TheseRules);
        }

        private bool CheckFolderForAccess(string FolderName)
        {
            DirectoryInfo di = new DirectoryInfo(FolderName);
            if (di == null)
                return false;

            DirectorySecurity acl = di.GetAccessControl(AccessControlSections.Access);
            if (acl == null)
                return false;

            AuthorizationRuleCollection TheseRules = acl.GetAccessRules(true, true, typeof(NTAccount));
            if (TheseRules == null)
                return false;

            return CheckACL(TheseRules);
        }

        private bool CheckACL(AuthorizationRuleCollection TheseRules)
        {
            foreach (FileSystemAccessRule ThisRule in TheseRules)
            {
                if ((ThisRule.FileSystemRights & FileSystemRights.Read) == FileSystemRights.Read)
                {
                    if (ThisRule.AccessControlType == AccessControlType.Deny)
                        return false;
                }
                // Run as many other checks as you like
            }

            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

在原来的这一行之前:

foreach (string fi in Directory.GetFileSystemEntries(DirName,"*", SearchOption.AllDirectories))

当时

foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.AllDirectories))

我将其更改为Directory.GetFileSystemEntries,因为我也想在子目录中检查拒绝访问。

1 个答案:

答案 0 :(得分:0)

我建议使用递归调用在这个问题上逐一查找目录,以便检查目录访问权限,具体限于检查中的某个目录。

在执行递归调用之前,您可以使用DirectoryInfo.GetDirectories检查目录是否有子目录。

这样,代码更清晰,只需在无法访问时传递目录。

此外,您可以使用Directory.GetAccessControl()检查您是否有权访问目录。