Why does FindFirstFileExFromApp throw a Win32Exception when searching root directories. C:\, D:\ etc.

Groovykool 206 Reputation points

When using FindFirstFileExFromApp to search a root directory like C:\ with an lpFileName set to C:\* or C:\*.* it throws a Win32Exception?

lpFileName's like C:\somefolder\* work fine.

The folders are picked and added to the Future Access List.

Example Repo https://github.com/groovykool/FindFirstFileExFromAppException.git

Thanks for any help.

Universal Windows Platform (UWP)
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,926 Reputation points


    Welcome to our Microsoft Q&A platform!

    As described in this https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt846625(v%3Dvs.85):

    If the string ends with a wildcard, period, or directory name, the user must have access to the root and all subdirectories on the path.

    When you access some folders, some single folder permissions are obtained through StorageApplicationPermissions, but when the target is a disk drive, there are still some sensitive folders with higher permissions, and permissions cannot be obtained this way.

    So the win32 error you encountered should be caused by a permission problem. The description of this error is Access violation reading location, which may access an inaccessible address.

    You can consider limiting the scope of your application's access. If you just want to get the file counts in the directory, you can consider using this method:

    Folder.Text = folder.Path;  
    var items = await folder.GetItemsAsync();  
    count = Convert.ToUInt32(items.Where(p => p is StorageFile).Count());  


    1 person found this answer helpful.