0

I am working on a small UWP app that will take pictures and video and save it at a desired location on the PC.

Here is the code base I am using https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/CameraStarterKit/cs/MainPage.xaml.cs

When I try to initialize StorageFolder class with a desired path, it comes out as null. It only supports to initialize with following paths

var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Documents);
var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);
var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);

Here is my code:

private StorageFolder _captureFolder = null;
 _captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder;
var file = await _captureFolder.CreateFileAsync("SmartPhoto.jpg",
                    CreationCollisionOption.GenerateUniqueName);
var picturesLibrary = await Task.Run(() =>
                    System.IO.File.Move(file.Path, @"C:\Temp\Pictures\" + file.Name));

Since StorageFolder is not initializing for C:\Temp\Pictures, I tried to move that file from KnownLibraryId.Pictures to C:\Temp\Pictures and that fails too,

System.UnauthorizedAccessException: 'Access to the path 'C:\Temp\Pictures' is denied.'

enter image description here

HaBo
  • 13,999
  • 36
  • 114
  • 206

1 Answers1

2

This is by design, an UWP app can not - by default - access all locations on your hard drive...

It only has a specific set of folders allowed, but you can enable more by using the file picker or adding capabilities.

As mentioned in the docs here : https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions

Universal Windows Apps (apps) can access certain file system locations by default. Apps can also access additional locations through the file picker, or by declaring capabilities.

Depechie
  • 6,102
  • 24
  • 46
  • So can I override that behavior and access the path I would want to? Is there an alternate approach? File picker is not an option for our requirement. This app is part of a other app, so the path will be derived/configured from main app. – HaBo Oct 29 '18 at 16:47
  • I'm not all that familiar with the extra capabilities feature ( because I have not used it myself yet ), but as far as I know; if the app does not have to be released through the Microsoft store you can circumvent this limited behaviour! Maybe readup on Broader file-system access here https://msdn.microsoft.com/en-us/magazine/mt846651 – Depechie Oct 29 '18 at 19:46
  • Ha more details can be found in this StackO similar question : https://stackoverflow.com/questions/33082835/windows-10-universal-app-file-directory-access – Depechie Oct 29 '18 at 19:47