Get all files from Assets Windows Store Application - c#

I am developing a Windows Store application and have about 20 images in the Assets/Backgrounds folder.
How can I get those files from C#?
I only need the file path, for example I need to create a List of strings object with file path. For example the list can be in the format of
List<string> files = new List<string> {"Assets/Backgrounds/file1.jpg",
"Assets/Backgrounds/file2.jpg", ...};
Thanks

First you need To know the Path of your Contents
string ImagesFile = #"Assets/Backgrounds/file1.jpg";
Then you Can Access The file like This:
StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await InstallationFolder.GetFileAsync(ImagesFile);

This contains all the files in your application package
ResourceManager.Current.MainResourceMap
Use Linq to filter those you need

Related

Detect Folders or Files on Gridview on UWP

I have files and folders in the "koleksibuku" folder. I want to show folders and files into the gridview. How do I detect whether in the "koleksibuku" folder there are folders or files?
Note:
When a folder using the image:"ms-appx:///images/folders_png8761.png"
When a file using data binding
Folder created by the user with the folder name in accordance with the wishes of the user
You can get a list of subfolders by calling GetFoldersAsync() on a StorageFolder.
Here's how to use it to get a list of folders from the music folder (requires "Music Library" capability).
var downloadsFolder = Windows.Storage.KnownFolders.MusicLibrary;
foreach (var folder in await downloadsFolder.GetFoldersAsync())
{
Debug.WriteLine($"Folder: {folder.DisplayName}");
}
Assuming you've created your "koleksibuku" folder in your apps local folder you would get it with something like this.
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var myFolder = await localFolder.GetFolderAsync("koleksibuku");
myFolder.GetFoldersAsync();

Getting all files in UWP app folder

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.
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();
var storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Assets");
var files = await storageFolder.GetFilesAsync();
https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata

Substring absolute path from known folder StorageFile

I'm working on a WinRT app that uses a local API.
This API returns me some path that looks like this:
C:Users\GB\Documents\MyDocs\DIVERS\fond.png
As you know, I need to use the StorageFolder and StorageFile to use the files that are in the Documents Folder.
I was wondering if there would be a good way to substitute the "known Folder" path for the path I get from the API. It would look like this:
MyDocs\DIVERS\fond.png
I'd like to do it in a nice way, not using splitting by "Documents" because it could get me in trouble if the user name one of his sub folders Documents.
var yourString = #"C:Users\GB\Documents\MyDocs\DIVERS\fond.png";
string knownFolder = #"C:Users\GB\Documents\";
yourString = yourString.Replace(knownFolder, "");
YourMethod($"{knownFolder}{yourString}");
Keep coding!

Simply get files from internal project folder [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
how to populate a listbox with files in the project of my windows phone 7 application
I'm a newbie on C# and this is annoying me a lot.
My application load a set of images from a folder that I simply created on the Solution Explorer called Images. I can see these images if I use it hardcoded with URIs and stuff, but what I want to do is to take these images names dinamycally and then load it. I have seen some questions like it but couldnt solve my problem. I'm trying like this:
DirectoryInfo directoryInfo = new DirectoryInfo(#"/Images");
foreach (FileInfo file in directoryInfo.GetFiles()) {
photos.Add(new Photo() { FileName = file.FullName, PhotoSource = GetImageSource("Images/" + file.FullName) });
}
The directoryInfo is always set as null. My project hierarchy as shown in Solution Explorer is like:
Project
Main.xaml
Maim.xaml.cs
Images
1.jpg
2.jpg
...
Thanks in any help.
From MSDN:
For a Windows Phone application, all I/O operations are restricted to
isolated storage and do not have direct access to the underlying
operating system file system or to the isolated storage of other
applications.
So you can't access your Images folder in the manner you'd like.
Since you can't add images dynamically to your XAP anyway, the images available will be constant. It appears you will just have to add the URIs manually:
BitmapImage myImage = new BitmapImage(new Uri("Images/myImage.png", UriKind.Relative));
If you've got different images for different locales, you could include the image name/path in a resources file and then create them from there.
Alternatively if you have a set of default images and will then have some user-added ones, and you'd like to iterate over all of those, you could write your defaults from your Images folder into IsolatedStorage at first start-up. See here for details on using IsolatedStorage. You can iterate over directories and files within the apps IsolatedStorageFile (see the methods available).
Maybe you want to use
string path = Path.Combine(Environment.CurrentDirectory, "Images");
foreach (string file in Directory.GetFiles(path)
{
photos.Add(new Photo {FileName = file, PhotoSource = GetImageSource(Path.Combine(path, Path.GetFileNameWithoutExtension(file))});
}
which returns a List of the Contents of the Folder.
Path.Combine combines single strings to a full Path (So you don't need to worry about the Backslahes.
The Path Namespace is pretty underrated, i'd suggest you to take a close look to it since it will save you much time.

How to get a list of files by date order in WinRT/Windows 8

I'm trying to get a list of files in date order in a Metro App in C#
I thought this code should do it,
var queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, new[] { ".xml" });
queryOptions.FolderDepth = FolderDepth.Deep;
StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Recent", CreationCollisionOption.OpenIfExists);
StorageFileQueryResult query = folder.CreateFileQueryWithOptions(queryOptions);
var files = await query.GetFilesAsync();
but this gives me the following error:
WinRT information: The requested enumeration option is not available
for this folder because it is not within a library or homegroup. Only folders within a library or a homegroup support all enumeration options.
Is there a way to get a list of files in date order when reading files from directories inside the Local folder?
You could recover the files and then use LINQ to Objects to perform the sorting for you.

Categories