[英]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.