-1
foreach(DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
{
    try
    {
        string[] files = Directory.GetFiles(d.RootDirectory.ToString(), fileName, SearchOption.AllDirectories);

        foreach(string file in files)
        {
            Console.WriteLine(file);
        }
    }
    catch(exception ex)
    {

    }
}

It seems simple enough, except I keep getting System.unauthorizedaccessexception.

So I tried to write a linq statement to check all files that don't have system flag and return it to a list.

var folderFound = new DirectoryInfo(d.RootDirectory.ToString())
   .GetFiles()
   .Where(f => !f.Attributes.HasFlag(FileAttributes.System) && f.Name == fileName)
   .Select(f => f.Name)
   .ToList();

But its not working quite right. It wont check all directories as well. How do i solve my problem?

I did not solve my problem Couldnt use a linq statement

foreach(DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
                    {

                        Stack<DirectoryInfo> dirstack = new Stack<DirectoryInfo>();

                        dirstack.Push(new DirectoryInfo(d.RootDirectory.ToString()));

                        while(dirstack.Count > 0)
                        {
                            DirectoryInfo current = dirstack.Pop();

                            foreach (DirectoryInfo di in current.GetDirectories())
                            {
                                if((di.Attributes & FileAttributes.System) != FileAttributes.System)
                                {
                                    dirstack.Push(di);
                                }
                            }


                            foreach (FileInfo f in current.GetFiles())
                            {
                                if (f.Name == fileName)
                                {
                                    fList.Add(f);
                                }
                            }
                        }

I am trying the one in the comments below, but as soon as i hit

var dirs = di.EnumerateDirectories();

it is throwing System.UnauthorizedAccessException. I don't have ability to even read if I have ability to manipulate. Is my guess at what is going on.

  • Possible duplicate of [Ignore folders/files when Directory.GetFiles() is denied access](https://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access) – mjwills Apr 02 '18 at 22:20
  • Unless this is a learning exercise, there are tools that specifically do this type of thing. Checkout indexed search tools. – P.Brian.Mackey Apr 03 '18 at 01:59

1 Answers1

1
var folderFound = new DirectoryInfo(d.RootDirectory.ToString())
   .GetFiles("*",SearchOption.AllDirectories)
   .Where(f => !f.Attributes.HasFlag(FileAttributes.System) && f.Name == fileName)
   .Select(f => f.Name)
   .ToList();

Update try this recursive method. found somewhere on stackoverflow. this will catch and continue to remaining.

public static List<string> GetAllAccessibleFiles(string rootPath, List<string> alreadyFound = null)
    {
        if (alreadyFound == null)
            alreadyFound = new List<string>();
        DirectoryInfo di = new DirectoryInfo(rootPath);
        var dirs = di.EnumerateDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            if (!((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden))
            {
                alreadyFound = GetAllAccessibleFiles(dir.FullName, alreadyFound);
            }
        }

        var files = Directory.GetFiles(rootPath);
        foreach (string s in files)
        {
            alreadyFound.Add(s);                
        }

        return alreadyFound;
    }
Krishna
  • 529
  • 3
  • 13
  • System.UnauthorizedAccessException: 'Access to the path 'B:\$RECYCLE.BIN\S-1-5-21-745477385-3434684888-2944739576-1003' is denied.' – sublimeaces Apr 02 '18 at 21:33
  • you should run your visual studio as administrator. try that and let me know – Krishna Apr 02 '18 at 21:36
  • i added !f.attributes.hasflag(fileAttributes.hidden) didn't help is it because i need FOLDER attributes too? – sublimeaces Apr 02 '18 at 21:37
  • ran as admin, this time i recieved System.UnauthorizesAccessException. Access to the path B:\System Volume Information is denied. Then i got a Visual studio pop up saying Exception has been encountered This may be caused by an extension. You can get more info in your appdata roaming activitylog.xml – sublimeaces Apr 02 '18 at 21:39
  • what is your aim of this code? should we skip if we cannot read specific file or folder? – Krishna Apr 02 '18 at 21:41
  • so it's checking a known path where an exe will create a folder. It is reading in that folder name and changing it into foldername.exe and then checking the whole computer for any instance of that foldername.exe it then does some more stuff to that like create a backup etc. – sublimeaces Apr 02 '18 at 21:43
  • Your updated works pretty good except i still am getting the exception UnauthorizedAccesException thrown at Var dirs = di.enumerateDirectories(); I cannot force this application to be run in admin mode because it is production and can't force them too do it either. is there anyway i can do a try catch and then just ignore it?so my big problem is that i cannot even read the file to check if i can't read the file. – sublimeaces Apr 03 '18 at 12:25
  • turns out i had a setting in visual studio checked to always throw it instead of discard it. rip me Thanks for your help. – sublimeaces Apr 03 '18 at 15:01