简体   繁体   English

如何在搜索时跳过被拒绝访问的目录或文件?

[英]How can i pass over a directory or a file that is access denied when searching?

I tried to add try and catch bit it didn't help it's just stop and nothing happen. 我试图添加尝试并抓住它并没有帮助它只是停止而没有任何反应。 I see the form1 and it finish after less then a second. 我看到了form1,它在不到一秒钟的时间内完成了。 Something is wrong with the try and catch. 尝试和捕获有问题。 How can i pass over a directory or a file that are 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;

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)
            {
                try
                {
                    foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.AllDirectories))
                    {
                        if (string.Compare(fi.Extension, ".cs", 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(" ");
                                    }
                                }
                            }
                        }
                    }
                }
                catch(Exception ee)
                {

                }
                w.Close();
            }
            return findLines;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Check the ACL (Access Control List) / AuthorizationRuleCollection of the Path. 检查路径的ACL(访问控制列表)/ AuthorizationRuleCollection。

You need to walk through the "FileSystemAccessRules" and accumulate what's allowed and denied. 您需要遍历“ FileSystemAccessRules”并累积允许和拒绝的内容。 Then interpret if the user is a member of that group for each rule. 然后,为每个规则解释用户是否是该组的成员。 This should get you started: 这应该使您开始:

//using System.Security.AccessControl;  
//using System.Security.Principal;


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;
}

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

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