I'm developing metro app using C# and XAML,As we all know we can search music files in music library using below code.
IReadOnlyList<IStorageItem> itemsList = await KnownFolders.MusicLibrary.GetItemsAsync();
What about music files in other drives(partitions) like D:,E: etc..Is there any way to search other drives for music files?
Brief: I want to search .mp3 files from my hard-drive and display them in Gridview, is there any efficient way to achieve this? Please help me.
It´s possible to search within other folders or drives, but it´s required to use the FolderPicker to select the StorageFolder...
async Task<IEnumerable<StorageFile>> FindMusicFiles()
{
var folderPicker = new FolderPicker()
{
CommitButtonText = "Yippie!",
SuggestedStartLocation = PickerLocationId.ComputerFolder,
ViewMode = PickerViewMode.List
};
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFileQueryResult queryResult = folder.CreateFileQuery();
queryResult.ApplyNewQueryOptions(new QueryOptions(CommonFileQuery.OrderByName, new[] { ".mp3" }));
IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync();
return files;
}
return new StorageFile[] { };
}
Windows Store apps are restricted to search within KnownFolders or LocalFolder of the application, so you cannot search within other drives
It's also possible for the app to remember the folder(s) you've selected with the FolderPicker, see my post on Windows 8 App and access to file system where I've explained how to use FutureAccessList...
Related
I am trying to save an captured photo into a folder as following:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
StorageFile photo = await
captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null)
{
// User cancelled photo capture
return;
}
StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
However, I cannot find the ProfilePhotoFolder in my file system. Could any one please tell me where is this folder. This is an Universal Windows project.
When you write to UWP LocalFolders the data is placed into Isolated Storage which is only accessible by your application, and thus you won't simply be able to fire up Windows Explorer and start digging into the files.
The recommended way for Windows 10 devices is to set your device to Developer Mode then use the web browser interface they've created (see https://blogs.windows.com/buildingapps/2016/06/08/using-the-app-file-explorer-to-see-your-app-data/#6O50PWljxSKfKCAm.97).
Try:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
Docs
I have a folder, let's say "C:\Downloads\Comic" and I would list all the files in this Directory, but I didn't find a way how.
The only way I found is with the help of a FolderPicker.
private async Task PickDirAndListfiles()
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
var files = await folder.GetFilesAsync();
foreach (var storageFile in files)
{
Debug.WriteLine(storageFile.Name);
}
}
}
But I know where the folder is and I don't want to pick it first. I want just this one folder!
How can I simply list my folder?
If you create this folder from your app you can use :
Windows.Storage.DownloadsFolder
and then you'll be able to access it.
If it's an already existing folder you'll need to ask the user to pick the folder/file and use :
StorageApplicationPermissions.FutureAccessList
to get a token that will be used for retrieving the corresponding folder/file without asking the user again and again to pick an item.
Alternatively you can declare capabilities to allow your app picking files without user interaction : Declaring capabilities
What is the right place for my Windows 10 IoT UWP app to persist temporary working data considering the device is occasionally turned off ? I have been researching about saving files on the SD card and reading from it, but it's been tough. Not even the code below is running, for the line IReadOnlyList<StorageFile> files = await folder.GetFilesAsync(); is causing the task to abort without raising any error. It's VS 2015 on Raspberry Pi 2.
public async void pop()
{
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
foreach (StorageFile sf in files)
{
if (sf.Name.EndsWith(".mtk"))
{
var file = await folder.GetFileAsync(sf.Name);
var readFile = await Windows.Storage.FileIO.ReadTextAsync(file);
nextFileContents = readFile.ToString();
break;
}
}
}
If you read Microsoft Documentation about access uSD on Windows 10 (here link)
you have to do two tasks:
specify the removableStorage capability in the app manifest file and
register to handle the file extensions associated with the type of media that you want to access
read the documentation and enjoy
I'm trying to play songs from a local machine by using the directory path to the song.
MediaElement.Source = new Uri(#"D:\Music\Artist\Album\Song.mp3", UriKind.Absolute);
Is this even possible to get to work or can Windows 8 apps only use URI schemes like mss-appx: to access package data?
When I try and run the code I get a message on the MediaElement control "Invalid Source"
Windows Store apps do not have full access to the file system. They can directly access (by path) only limited locations (i.e. their install and applicationdata folders).
The MediaElement can load items from paths it can directly access, but this is not generally useful since these locations have URIs (ms-appx: and ms-appdata:) which will target the right location regardless of what the actual Path is.
Typically songs are in the Music library, which the MediaElement cannot directly access. It can get brokered access through the MusicLibrary capability, but that doesn't allow access by path. The app will need to get to the file through the KnownFolders object:
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder musicLib = Windows.Storage.KnownFolders.MusicLibrary;
StorageFile song = await musicLib.GetFileAsync(#"Artist\Album\Song.mp3");
var stream = await song.OpenReadAsync();
me.SetSource(stream, stream.ContentType);
}
If the song isn't in a library that can be permitted by capability then the user will need to grant permission through a FolderPicker or such. The user can pick the root of the music location and the app can cache that with the Windows.Storage.AccessCache classes so the user doesn't need to pick the folder multiple times or individually pick files.
I discuss this in more detail in my blog entry Skip the path: stick to the StorageFile
You need to use the file open picker to select a file in D drive.
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStreamWithContentType content = await file.OpenReadAsync();
Debug.WriteLine("Content Type: " + content.ContentType);
player.SetSource(content, content.ContentType);
This link has the answer (in case you can use the application folder)
MediaElement.Source = new Uri("ms-appx-web:///Assets/Song.mp3", UriKind.Absolute);
I'm trying to build a metro-app which shall load an image file from another computer in the same homegroup (all computers use windows 8 x64 with working homegroup). All samples I found do not use subfolders or use the filepicker.
Since all my images are in the same folder and I know their names I do not want to use the filepicker.
I activated "Pictures Library" in the appxmanifest and I can list the directories/computers in the homegroup but I'm stuck in opening files or subfolders.
Here's what I did:
var folder = await Windows.Storage.KnownFolders.HomeGroup.GetFolderAsync("homegroupname");
foreach (var a in await folder.GetFoldersAsync())
{
Debug.WriteLine(a.Name.ToString());
}
This gave me a list of the computers of the homegroup (as expected).
Here's what I tried without success:
folder = await Windows.Storage.KnownFolders.HomeGroup.GetFolderAsync(#"homegroupname\computername");
folder = await folder.GetFolderAsync(#"computername");
These attempts didn't work and I ran out of ideas. Do I have to allow the folder somewhere? Is my way of opening the (sub-)folders the right one?
I did it with the following (nearly intuitive) approach:
I create a CommonFileQuery for files of the right type and choose the one with fitting name.
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
fileTypeFilter.Add(".png");
var queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
var query = KnownFolders.HomeGroup.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();
StorageFile file = fileList.FirstOrDefault(x => x.Name == "123_123.jpg");