繁体   English   中英

如果访问被拒绝,我该如何检查子目录和/或文件是否被拒绝?

[英]How can i check also sub directories and/or files for access denied and to pas over if access denied?

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

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,因为我也想检查子目录中也拒绝访问。

我建议使用递归调用来逐一查找此问题,以使对目录访问的检查专门限于检查中的某些目录。

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

这样,代码就更干净了,您只需要在无法访问目录时传递目录。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM