I need to save app files to plugged SD card. I'm using UWP and Windows 10.
MSDN tells how to do it with Windows Libraries.
var myPicsLibrary = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
await myPicsLibrary.RequestAddFolderAsync();
RequestAddFolderAsync() shows file picker, where user can choose folder to add into Pictures. In my case it's a folder, created on SD card.
Is there a way to do this thing without file picker dialog?
I'm trying to do like this:
var myPicsLibrary = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
// Get the logical root folder for all external storage devices.
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
// Get the first child folder, which represents the SD card.
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
var folder = await sdCard.CreateFolderAsync("MySDCardFolder");
myPicsLibrary.Folders.Insert(myDocs.Folders.Count+1, folder); // NotImplementedException: 'The method or operation is not implemented.'
myPicsLibrary.Folders.Add(folder); // NotImplementedException: 'The method or operation is not implemented.'
Or maybe I can do the same without using Windows Libraries directly working with SDCard?
Thanks a lot!
EDIT:
In the another hand my question sounds like "How to save files to plugged SD card?"
The StorageLibrary.Folders gets the folders in the current library, it return the IObservableVector of the StorageFolder. When we add the folder into the IObservableVector, it will not change the folder in files system. It will throw the "The method or operation is not implemented." exception.
We should be able to get the name of the folder, and create the folder uses that name. Then we can StorageFile.CopyAsync method to copy the file that in your folder.
For example:
public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer, string desiredName = null)
{
StorageFolder destinationFolder = null;
destinationFolder = await destinationContainer.CreateFolderAsync(
desiredName ?? source.Name, CreationCollisionOption.ReplaceExisting);
foreach (var file in await source.GetFilesAsync())
{
await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.ReplaceExisting);
}
foreach (var folder in await source.GetFoldersAsync())
{
await CopyFolderAsync(folder, destinationFolder);
}
}
Then we can use the CopyFolderAsync method to copy the folder in the Picture Library.
var myPicsLibrary = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
var myfolder = myPicsLibrary.Folders[0];
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
var folder = await sdCard.CreateFolderAsync("MySDCardFolder");
await CopyFolderAsync(folder, myfolder);
As I get from your question you need to create a Folder in PicturesLibrary .
You can use the code below to add a folder into PicturesLibrary
await Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync("MySDCardFolder");
if you want to make it on SD Card only Maybe KnownFolders.RemovableDevices Should be helpful . I didn't used KnownFolders.RemovableDevices yet but if you have any issue let me know to test it on my phone directly
Related
I am trying to copy .csv files created in the local folder in Windows 10 iot core. I have tried few ways but no luck. My latest code is as follows:
string aqs = UsbDevice.GetDeviceSelector(0x045E, 0x0611);
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs);
UsbDevice usbDevice;
try
{
if(myDevices == null)
{
return;
}
usbDevice = await UsbDevice.FromIdAsync(myDevices[0].Id);
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder sourcef = await localFolder.CreateFolderAsync("My Data", CreationCollisionOption.OpenIfExists);
IReadOnlyList<StorageFile> filel = await sourcef.GetFilesAsync();
StorageFolder removableDevices = KnownFolders.RemovableDevices;
//var externalDrives = await removableDevices.GetFoldersAsync();
//var drive0 = externalDrives[0];
//var destFolder = await removableDevices.CreateFolderAsync("My Data", CreationCollisionOption.GenerateUniqueName);
foreach (StorageFile file in filel)
{
await file.CopyAsync(removableDevices);
}
}
In the above i get an exception on:
usbDevice = await UsbDevice.FromIdAsync(myDevices[0].Id);
'myDevices[0].Id' threw an exception of type 'System.ArgumentOutOfRangeException'
I have tried checking if this is null and it is not null.
The aim of this is to basically copy few text files from Local Folder to the USB drive.
The usb storage can not be found by using UsbDevice.GetDeviceSelector, the methods in Windows.Devices.Usb namespace are used for valid WinUSB device which has a compatible id of USB\MS_COMP_WINUSB, but a usb storage will not include compatible id.In fact, KnownFolders.RemovableDevices is enough to access the device.
I have problem while using PCLStorage for Xamarin.Forms. I try to create new folder to save my canvas as image and it don't even want create that new folder. I don't know what is wrong because i already checked many guides how storage files on Xamarin and i didn't found any solution. My code so far is:
saveButt.Clicked += async (sender, args) =>
{
String folderName ="testFolder" ;
IFolder folder = FileSystem.Current.LocalStorage;
folder = await folder.CreateFolderAsync(folderName, CreationCollisionOption.ReplaceExisting);
ExistenceCheckResult folderexist = await folder.CheckExistsAsync(folderName);
if (folderexist == ExistenceCheckResult.FolderExists)
{
await DisplayAlert("Alert", "Exists", "OK");
}
};
I'm trying to create a file explorer for windows 10 mobile
I need to launch files with default app from pathes (not installed directory)
ex. "d:\test.pdf"
Here's what I've tried:
string p = #"a.jpg";
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
StorageFile file = await externalDevices.GetFileAsync(p);
var success = await Launcher.LaunchFileAsync(fff);
you can use Windows.System.Launcher API to launch the default handler for a file you can read more here
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = #"images\test.png";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
If you want to access files from SD card, please make sure that you have read the What you can and can't access on the SD card section on document.
Your app can only read and write files of file types that the app has registered to handle in the app manifest file.
Your app can also create and manage folders.
Make sure that you check "Removable Storage" in the capabilities of the appxmanifest.
You must declare a file type association in the Package.appxmanifest in order to read files from the SD card. The following is an example of one that I set up:
Then, I used the following code sample to launch a picture which is contained in "New folder" on SD card successfully.
string p = #"1.jpg";
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
var rootfolder = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
var folder = await rootfolder.GetFolderAsync("New folder");
var file = await folder.GetFileAsync(p);
var success = await Launcher.LaunchFileAsync(file);
I am trying to check if a folder exist .. if it doesn't i will need to create a folder. My code seems to work if the folder does not exist and create a folder... but subsequently after create the folder.. it runs into exception handler..
I am not sure where went wrong.. please advise.
Thanks.
StorageFolder externalDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
StorageFolder usbStorage = externalDrives[0];
String folderName = "Recordings";
String fileName = DateTime.Now.ToString();
if (!Directory.Exists(folderName))
{
await usbStorage.CreateFolderAsync(folderName);
}
await usbStorage.GetFolderAsync(folderName);
StorageFolder recordFolder = await usbStorage.GetFolderAsync(folderName);
StorageFile recordFile = await recordFolder.CreateFileAsync("Recording -" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".mp3", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
RecordStatus.Text = "File setup OK ... ";
Firstly, using Directory.Exists to check whether a fold exist is not suitable here. The path parameter of Directory.Exists method is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. But in your code, the folderName is not relative to your working directory(in fact,your working directory is Windows.ApplicationModel.Package.Current.InstalledLocation).
Secondly, in UWP, the method of CreateFolderAsync has an overload method with CreationCollisionOption parameter.
Please change this part in your code
if (!Directory.Exists(folderName))
{
await usbStorage.CreateFolderAsync(folderName);
}
To
await usbStorage.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
It will be ok.
In my Windows Store project I have some .jpg files in my Assets folder. How can I dynamically get all the .jpg files from that folder?
I've tried:
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync("ms-appx:///Assets/");
var fileList = await folder.GetFilesAsync(CommonFileQuery.DefaultQuery);
Must work somehow like this (with a different CommonFileQuery) but this is not working for me. (System Exception at line 1 - wrong path name).
Second question: how do I test if my Assets folder contains "movie.mp4"?
Thank you!
You can use Directory.EnumerateFiles(sourceDirectory, "*.jpg", SearchOption.AllDirectories);
Question 2:
You can use File.Exists(filePath);
Edit
As per your comment, I found this which uses GetFileFromApplicationUriAsync instead of GetFolderFromPathAsync
or
StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var files = installedLocation.GetFolderFromPathAsync("Assets");
Q2 seems like you can use
TryGetItemAsync
This is the complete solution for my scenario:
Get Files within a project folder of a certain file type.
Check if a file exists within a project folder.
1:
StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder subFolder = await installedLocation.GetFolderAsync("Assets");
subFolder = await subFolder.GetFolderAsync("Images");
List<String> fileType = new List<String>();
fileType.Add(".jpg");
var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, fileType);
var query = subFolder.CreateFileQueryWithOptions(queryOptions);
var fileList = await query.GetFilesAsync();
2:
try
{
var video = await subFolder.GetFileAsync("Video.mp4");
}
catch (FileNotFoundException)
{
Debug.WriteLine("No Video found");
}