question

Groovykool-9087 avatar image
1 Vote"
Groovykool-9087 asked ·

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

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. Tony

uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichardZhang-MSFT avatar image
1 Vote"
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

As described in this https://docs.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());

Thanks.

· 9 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

No not really.

FindFirstFileExFromApp docs say:

"Searches a directory for a file or subdirectory with a name and attributes that match those specified. The behavior of this function is identical to FindFirstFileEx, except that this function adheres to the Universal Windows Platform app security model."

So picking a root folder should give the same access as GetFilesAsync. And it does for most folders.

The reason for using FindFirstFileExFromApp instead of GetFilesAsync is that it is at least 1000 times faster. GetFilesAsync is useless for enumerating a large number of files.

Regards,

Tony

0 Votes 0 · ·

Hi, I have reported this issue, and if the engineer has a new reply, I will post it here in time. It needs to be verified whether the FindFirstFileExFromApp method can provide the same permissions as the StorageFolder.GetItemsAsync method. But from the current point of view, their access rights may not be the same.

1 Vote 1 · ·

Thanks.

More information. When using FindFirstFileExFromApp to search a OneDrive folder like:

C:\Users\username\OneDrive\Pictures\

Results in an access denied(Returns -1) but does not throw an exception. The OneDrive folder is on the FutureAccessList

Regards,

Tony

0 Votes 0 · ·
Show more comments

As a test can you try adding the restricted capability for broadFileSystemAccess? You'll need to add this namespace to your package: xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">;

Then add rescap to the list of IgnorableNamespaces like so: IgnorableNamespaces=" rescap". e.g. IgnorableNamespaces="uap mp rescap"

And add the restricted capability to the Capabilities section: <rescap:Capability Name="broadFileSystemAccess" />

You can read more about broadFileSystemAccess and the additional requirements here: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations

0 Votes 0 · ·

Adding broadFileSystemAccess eliminates the win32 exception for root drives and the access denied for onedrive folders.

However some folders on the drive still return (-1). Not sure which ones yet. I would need to investigate more.

Regards, Tony

1 Vote 1 · ·