Reputation: 11
var path = @"C:\Users\Admin\Downloads\JsonData.json";
var rootObj = JsonConvert.DeserializeObject<Rootobject>(
File.ReadAllText(path)
);
when I do this on the console application everything works, but on uwp it throw the following exception:
System.UnauthorizedAccessException: "Access to the path 'C:\Users\Admin\Downloads\JsonData.json' is denied."
Upvotes: 1
Views: 982
Reputation: 16554
There are two issues here, one is security (related to Sandboxing), the other is more important, in UWP you need to use the utilities in the Windows.Storage Namespace, access to the file system has been abstracted to support different runtime environments and to facilitate containerization and sandboxing of the runtime.
So even if all the permissions are correctly set, your code could work, but you should use the StorageFile
interface to access files as this guides you write code that is more stable and compatible across all UWP runtimes.
Ignoring the security side of things, you could access the same path using the Storage utilities:
Have a read of Create, write, and read a file
var path = @"C:\Users\Admin\Downloads\JsonData.json";
var folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(path));
var file = await folder.GetFileAsync(System.IO.Path.GetFileName(path));
var rootObj = JsonConvert.DeserializeObject<Rootobject>(
await Windows.Storage.FileIO.ReadTextAsync(file)
);
Locations that UWP apps can access
Universal Windows Platform (UWP) apps can access certain file system locations by default. Apps can also access additional locations through the file picker, or by declaring capabilities.By default, your app can only access files and folders in the user's Downloads folder that your app created. However, you can gain access to files and folders in the user's Downloads folder by calling a file picker (FileOpenPicker or FolderPicker) so that users can navigate and pick files or folders for your app to access.
File picker works because your app is not directly accessing any specific file path, instead the user can browse to wherever the use has access to, and when application permissions or elevated privelidges are required, the user relavant user interface prompts will be displayed for the user to respond to. The picker uses this information to retrieve access to the file for you, without having to code all of the above.
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads;
picker.FileTypeFilter.Add(".json");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
var rootObj = JsonConvert.DeserializeObject<Rootobject>(
await Windows.Storage.FileIO.ReadTextAsync(file)
);
}
The Downloads folder in the UWP is special interface on its own, as listed above By default, your app can only access files and folders in the user's Downloads folder that your app created. So usually we use the DownloadsFolder Class when we need to interact specifically with the user's downloads folder.
This post is worth a read: UWP access denied
The easy way to gain access to this folder is by enabling BroadFileSystemAccess, but some users security policies may not allow an app with this level of file system access to be executed.
For this reason I strongly advise against using this even in development environments, you will become complacent (read Lazy) and will not be prepared for other fundamental changes to the code that you may need to make before you can publish it.
It will also complicate the approval process to get your app through the store...
Upvotes: 2