c# UWP Save StorageFile Without Dialog
Asked Answered
S

3

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();
Subbase answered 10/3, 2016 at 8:44 Comment(10)
what type of file it is?Glassware
See msdn.microsoft.com/de-de/library/… for how to write to a file.Alkaloid
How did you get the current path - if that was from an existing StorageFile, can you not save via that object?Nephralgia
Take a look at MSDN.Blub
Actually, What i want to do is, Converting a base64 string to ByteArray and Storage File as .jpg or .png and saving this image to given path. User does not know anything about this process.Subbase
ı edited my question by giving code part that explains what i want to do.Subbase
What's the problem? You are writing the bytes to the file and then you show the FileSavePicker? Why?Curule
@Subbase Where are you want to save your StorageFile?Longcloth
@Curule I am defining the SuggestedFile of FileSavePicker as myfile to save it.Subbase
@Andrii Krupka Wherever i want. "C:\SavedImages" such as.Subbase
L
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

Laureenlaurel answered 10/3, 2016 at 14:37 Comment(1)
Can you, please, mark it as answer, for details looks here stackoverflow.com/help/someone-answersLaureenlaurel
S
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.

Subbase answered 10/3, 2016 at 10:12 Comment(0)
C
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.

Curule answered 10/3, 2016 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.