Reputation: 653
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. 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)
{
}
}
}
Upvotes: 0
Views: 241
Reputation: 1151
Check the ACL (Access Control List) / AuthorizationRuleCollection of the Path.
You need to walk through the "FileSystemAccessRules" and accumulate what's allowed and denied. 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;
}
Upvotes: 1