7

I am in trouble with some issue about FileSavePicker. Is there any solution about saving a StorageFile without showing any popup or dialog to ask user. I want to give the current path of the storage file from code behind.

var byteArray = Convert.FromBase64String(Base64);
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("file.jpg", CreationCollisionOption.ReplaceExisting);  
await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);
var savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("JPEG-Image", new List<string>() { ".jpg" });
savePicker.SuggestedSaveFile = file;
savePicker.PickSaveFileAsync();
alidrsn
  • 109
  • 1
  • 10

3 Answers3

11

...We have to use a few paths which are defined under 'Windows.Storage.KnownFolders'...

it is not so. In fact your app can access any folder on device but it will need additional permissions. The most straightforward way to obtain permission you should do next: 1) ask user to pick folder from FolderPicker 2) store selected folder to StorageApplicationPermissions.FutureAccessList After this your app can do anything with this folder.

Code that demonstrate how to obtain permissions:

var picker = new FolderPicker();
var pfolder = await picker.PickSingleFolderAsync();
StorageApplicationPermissions.FutureAccessList.Add(pfolder);

Code that demonstrate how to create file in desired folder:

var folder = await StorageFolder.GetFolderFromPathAsync("your path");
var file = await folder.CreateFileAsync("text.txt");
using (var writer = await file.OpenStreamForWriteAsync())
{
      await writer.WriteAsync(new byte[100], 0, 0);
}

But keep in mind that "your path" is folder or any of subfolder that was stored to StorageApplicationPermissions.FutureAccessList. More details here FutureAccessList

Sergii Apostol
  • 311
  • 1
  • 8
0

I want to thank you all for your interest. I figured out the solution. If you do not want to use any dialog or prompting you can not use FileSavePicker. Here are the simple codes you need to converting Base64 string to image and save.

var byteArray = Convert.FromBase64String(Base64);

StorageFile file = await Windows.Storage.KnownFolders.SavedPictures.CreateFileAsync(
                      "file.jpg", CreationCollisionOption.ReplaceExisting);

await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);

I guess there is no way to give specific path. We have to use a few paths which are defined under Windows.Storage.KnownFolders. If it is not, please give some information about it.

alidrsn
  • 109
  • 1
  • 10
0

StorageFile.GetFileFromPathAsync() is probably what you are looking for. I think the file must be created beforehand for this method to work. Be aware that creating the file in some locations might need additional permissions or is even impossible (I don't know how UWP and UAC interact).

Another possibility is to use Win32 APIs inside UWP apps. Maybe this can solve your problem if the desired API is available.

xmashallax
  • 1,673
  • 1
  • 17
  • 34