1
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."
Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
  • That’s a limitation of UWP you must use something described here: https://learn.microsoft.com/en-us/windows/uwp/files/ - or you can switch to WinUI instead of UWP - WinUI tries to remove those limitation in a project called Reunion - https://github.com/microsoft/ProjectReunion – Rand Random Aug 29 '20 at 17:02
  • When you say "When i do this" what do you mean ? Can you post the line of code / method that gives you the error ? – Soundararajan Aug 29 '20 at 17:55
  • var rootObj = JsonConvert.DeserializeObject(File.ReadAllText(@"C:\Users\Admin\Downloads\JsonData.json"));@Soundararajan – Araik Nikoghosyan Aug 29 '20 at 18:46
  • UWP apps by default do not have free access to the entire hard drive. The user needs to grant the app access to the file (say by using a picker). Alternatively you can claim broadFileSystemAccess but that will likely prevent it from being accepted by the store. – Raymond Chen Aug 29 '20 at 21:25
  • @RaymondChen thanks a lot but how can i do this? need to use Storage namespaces? – Araik Nikoghosyan Aug 29 '20 at 21:34
  • Storage namespace is the UWP style. This would be FileIO.ReadLinesAsync. – Raymond Chen Aug 29 '20 at 22:30
  • @RaymondChen thanks, everything worked out now I have an array of image links I need to show them on uwp I added through x bind but why can't I see the url property? – Araik Nikoghosyan Aug 30 '20 at 18:14
  • @AraikNikoghosyan you need to post a new question on SO that covers your new issue. your next response should be a link to the new issue, please include more code detail in the next one though ;) – Chris Schaller Aug 31 '20 at 01:41

1 Answers1

2

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)
              );

Regarding Security and App Permissions

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 approach

How Pickers Work

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)
                  );
}

Declaring Capabilities

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 concept works great when your app creates the files in that folder, and you want to user to have access to the files outside of your application domain, you get easy access to them and so do they.

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...

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
  • Thanks a lot, I found another solution, I just added the file to the folder where the program could not find. now I have one more problem I have a json that stores 3 arrays when I do deserialization, not all data is read, is there any solution? – Araik Nikoghosyan Aug 31 '20 at 21:29
  • You should really post that as a new question, with the file content and the code you are using to deserialize. Also posting the code you use to serialize can help, but its out of the scope of this question – Chris Schaller Aug 31 '20 at 22:33