简体   繁体   English

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

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

I want to search and pass over directories or files that are access denied also in sub directories. 我想搜索并传递子目录中也拒绝访问的目录或文件。

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)
        {

        }
    }
}

Before in the original this line: 在此行之前:

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

Was

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

I changed it to Directory.GetFileSystemEntries since i want to check for access denied also in sub directories too. 我将其更改为Directory.GetFileSystemEntries,因为我也想检查子目录中也拒绝访问。

I recommend to use recursive call to look up the directory one by one on this problem to make the check of the directory access specifically limited to certain directory in check. 我建议使用递归调用来逐一查找此问题,以使对目录访问的检查专门限于检查中的某些目录。

You can use DirectoryInfo.GetDirectories to check if a directory has a sub-directory before you do the recursive call. 您可以使用DirectoryInfo.GetDirectories在执行递归调用之前检查目录是否具有子目录。

This way, the code is cleaner, and you only need to pass a directory when it is not accessible. 这样,代码就更干净了,您只需要在无法访问目录时传递目录。

Also, you can use Directory.GetAccessControl() to check if you have an access to a Directory before hand. 另外,您可以使用Directory.GetAccessControl()事先检查是否有权访问目录。

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

相关问题 获取文件时如何检查访问被拒绝? - How can i check for access denied when getting files? 搜索所有子目录时如何忽略拒绝访问? - How can i ignore access denied when searching to get all sub directories? 如何避免或跳过被拒绝访问的目录? - How can i avoid or pass over a directory that is access denied? 如何阻止asp.net阻止访问文件然后在构建时抛出访问被拒绝的错误? - How can I stop asp.net from blocking access to files then throwing access denied errors on build? 如何轻松检查 .NET 中的文件是否被拒绝访问? - how can you easily check if access is denied for a file in .NET? 如何在搜索时跳过被拒绝访问的目录或文件? - How can i pass over a directory or a file that is access denied when searching? 如何检查是否拒绝对字符串路径的访问? - How do I check if an access to a string path is denied? 拒绝访问Web服务,但是我可以浏览到它 - access denied to a webservice, however I can browse to it 我正在尝试将一些文件从一个目录压缩到另一个目录,但我在其中一个目录上被拒绝访问? - I'm trying to compress some files from one directory to another but I'm getting access denied on one of the directories why? 尝试访问用户信息时如何避免访问被拒绝的异常? - How can I avoid an access denied exception when trying to access User Information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM