How can i pass over access denied directory or file when searching in files ?


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 (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(" ");                                      }                                  }                              }                          }                      }                  }                  w.close();              }              return findlines;          }            private bool checkforaccess(string pathname)          {              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 many other checks              }                return true;          }            private void form1_load(object sender, eventargs e)          {            }      }  }  

the problem how check access sub directories , files ? it's checking main directory dirname

but i'm getting access denied exception on directory on sub directory when it's getting line:

foreach (fileinfo fi in di.enumeratefiles("*", searchoption.alldirectories))

hi chocolade,

it's because using directoryinfo.enumeratefiles() needs security rights info. instead try using directory.getfilesystementries(); string of each file/folder , check access before getting info. 

does above resolve issue?  for more information, please refer how can pass on directory or file access denied when searching?

best regards,

kristin


we trying better understand customer views on social support experience, participation in interview project appreciated if have time. helping make community forums great place.
click here participate survey.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'