Windows universal application. Access to the path is denied

5.8k Views Asked by At

I'm building my first windows universal application.

I'm trying to acces a file but I receive this error:

Additional information: Access to the path 'C:\database\reservation.txt' is denied.

Code:

 await Task.Run(() =>
                {
                    string path = @"C:\database\reservation.txt";
                    string json = File.ReadAllText(path);
                    dynamic result = JsonConvert.DeserializeObject(json);

                    var urls = new List<string>();

                    foreach (var file in result.version.files)
                    {
                        urls.Add(file.url);
                    }
                });

How do I solve this?

I already tried to set all capabilities to true. But that's not working.

1

There are 1 best solutions below

0
On BEST ANSWER

You can't access all your files just like that in Windows Store apps. Please take a look at this MSDN page for list of locations, which your app is permited to use. Also to use most of them you will have to declare suitable Capabilities. If your file is outside this list and/or you haven't declared capabilities, you will get UnauthorizedException.

Generaly store apps shouldn't access files without user knowing about it - this is by design. If you want to access file in future, you can for example pick a file with FileOpenPicker and then access it by FutureAccessList or MostRecentlyUsed.