Getting all files in UWP app folder

2020-02-23 07:02发布

问题:

For UWP, it is easy to get all files in the app local folder as:

IReadOnlyList<StorageFile> files = await ApplicationData.Current.LocalFolder.GetFilesAsync();

You can now iterate on the files list and even get further info on individual files.

I would like a similar all-file-getter for an app folder, for instance, consider the /Assets folder where app *.png files are stored. Single file with a known name is no problem; I can refer to it quite easily as:

StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Assets/StoreLogo.png"))

My question is, therefore, is there a similar thing for getting all files in an app folder, such as /Assets folder? Logically, it should be something like StorageFile.GetFilesFromApplicationFolderUriAsync(new Uri(@"ms-appx:///Assets")) but unaware if an equivalent of the LocalFolder shown above exists.

回答1:

You can access you installation folder by using Package.InstalledLocation. Therefore your code can look like this:

StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets");
var files = await assets.GetFilesAsync();


回答2:

var storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Assets");
var files = await storageFolder.GetFilesAsync();

https://docs.microsoft.com/en-us/uwp/api/windows.storage.applicationdata