0

I'm searching for files in my C# program using the following function:

        static string[] getFiles(string path, string searchPattern, SearchOption searchOption)
    {
        string[] searchPatterns = searchPattern.Split('|');
        List<string> files = new List<string>();
        try
        {
            foreach (string sp in searchPatterns)
                files.AddRange(Directory.GetFiles(path, sp, searchOption));
            files.Sort();
        } catch (Exception ex){ System.Windows.Forms.MessageBox.Show(ex.Message); }

        return files.ToArray();
    }

When I search for files I pass in the following code to my function:

var myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

var files = getFiles(myDocuments, "*", SearchOption.AllDirectories);

Now when I execute the code I get the following error:

enter image description here

For some reason it's searching Documents/My Music instead of C:\Users\Test\Music. The error is occurring on Win7. I presume the cause of the problem is described in the following link (even tho I never upgraded my Windows): Microsoft Document Changes .My aim is to search all files inside the "My Documents" folder. This also includes searching all subdirectories such as My Music, My Pictures etc. Could anyone suggest some different code I can use or a solution to fix this problem?

Harry
  • 301
  • 1
  • 4
  • 9
  • Are you logged on as the "Test" user when you run that program? – Lasse V. Karlsen Dec 08 '18 at 20:37
  • No the program is behaving as it should. `Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)` should return `C:\Users\Test\Documents`. – Tobias Tengler Dec 08 '18 at 20:38
  • @LasseVågsætherKarlsen Yes. That's the only user on the computer. – Harry Dec 08 '18 at 20:38
  • @BoredomOverload Yes it does return that path but I guess something is wrong with the search algorithm. Specifically SearchOption.AllDirectories. – Harry Dec 08 '18 at 20:39
  • You cannot know how your directory's permission are set up, so your best bet is not to search all folders, but go folder by folder recursively yourself and stop going further with that folder if access is denied. – nemanja228 Dec 08 '18 at 20:40
  • Can you open that folder in Windows Explorer, while being logged on as that user? – Lasse V. Karlsen Dec 08 '18 at 20:40
  • @LasseVågsætherKarlsen No I can't. That's the thing, it's searching for a folder that doesn't exist. – Harry Dec 08 '18 at 20:44
  • @nemanja228 Thanks for the suggestion. I've considered doing that as a solution so I might as well work on it if my current code can't be fixed. However this SO thread (https://stackoverflow.com/questions/9830069/searching-for-file-in-directories-recursively/9830116) recommends using Directory.GetFiles which I'm doing right now. – Harry Dec 08 '18 at 20:47
  • `My Music` (localized) is in `Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);`. The Documents folder is really not a good starting point for a blind enumeration of Directories and Files. You have already found the `AccessDenied` problem. You'll also find the `ReparsePoints` almost-infinite-loop problem. – Jimi Dec 08 '18 at 21:44
  • GetFiles does not continue when you get exceptions when you do not have access to files/folders. See my solution at following posting on how to continue after an exception : https://stackoverflow.com/questions/53681860/cannot-list-all-the-sub-directories-in-linux-using-c-sharp/53683353#53683353 – jdweng Dec 08 '18 at 21:48

0 Answers0