簡體   English   中英

如何在搜索時跳過被拒絕訪問的目錄或文件?

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

我試圖添加嘗試並抓住它並沒有幫助它只是停止而沒有任何反應。 我看到了form1,它在不到一秒鍾的時間內完成了。 嘗試和捕獲有問題。 如何傳遞被拒絕訪問的目錄或文件?

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

        }
    }
}

檢查路徑的ACL(訪問控制列表)/ AuthorizationRuleCollection。

您需要遍歷“ FileSystemAccessRules”並累積允許和拒絕的內容。 然后,為每個規則解釋用戶是否是該組的成員。 這應該使您開始:

//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