1

I'm trying to read a JSON file with a web application on my localhost. I keep getting the same error:

Access to the path 'C:\Users\Admin\Documents\test\response.json' is refused. It seems like it can find the file but can't read it.

Here is how I try to read the file:

using (StreamReader reader = new StreamReader("C:\Users\Admin\Documents\test\response.json"))
{
    string fileContent = reader.ReadToEnd();
    JObject resultASJtoken = JObject.Parse(fileContent);
    return resultASJtoken;
}

I've also tried to change my right using icacls powershell command but it doesn't seems effective.

icacls "C:\Users\Admin\Documents\test\response.json" /grant Admin:F /T

If someone has an idea, I would really appreciate some help in this.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Romain
  • 15
  • 5
  • 1
    Is this the *exact* error message? On which line does it occur? What is `resourceMethod.Method`? – Klaus Gütter Apr 19 '22 at 12:23
  • 1
    Side note: you should always wrap the StreamReader in a `using` statement, otherwise it will not (immediately) release unmanaged resources like file handles. – Klaus Gütter Apr 19 '22 at 12:25
  • resourceMethod.Method is the path to my file. In my exemple, it's "C:\Users\Admin\Documents\test\response.json". The error is system.UnauthorizedAccessException and it occur at the first line during the string reader – Romain Apr 19 '22 at 12:32
  • @Romain kindly share the full exception on your question. – John Nyingi Apr 19 '22 at 12:36
  • System.UnauthorizedAccessException : Access to the path 'C:\Users\Admin\Documents\test\response.json' is denied. It's the exact message i get, i've just translated it from french sorry for the linguistic errors. – Romain Apr 19 '22 at 12:46
  • Access may be denied because you tried to read some other user's files or because you tried to read an already opened file. Post the *exact* message without translating it – Panagiotis Kanavos Apr 19 '22 at 13:03
  • 3
    In any case, you can't just change the permissions of another user's file. If you can't even read from it, you certainly can't change its permissions. A web application has *no business* reading files outside its own folder anyway. This way a hacked app can't damage the rest of the system. IIS explicitly restricts app pool accounts to only read from the web app's folder. This can be changed if there are valid reasons – Panagiotis Kanavos Apr 19 '22 at 13:05
  • What are you trying to do? And what kind of application is this? ASP.NET Core ? ASP.NET Framework? Does the app really have to read from some user's `Documents` folder? In ASP.NET Core you can read files from different locations *without* specifying their absolute path using [File Providers](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers?view=aspnetcore-6.0) – Panagiotis Kanavos Apr 19 '22 at 13:09
  • The exact message without translation : L'accès au chemin d'accès 'C:\Users\Admin\Documents\test\response.json' est refusé. @PanagiotisKanavos I trie to implement a feature for my web application (ASP. NET framwork) to read a json file that is on the user's computer. – Romain Apr 19 '22 at 13:16
  • You can't do that. I explained why. Especially when that user is the administrator of the machine. I asked what you actually want to do though. `Implement a feature` isn't a problem description. If you want to use specific files, why not copy them to the web app's data directory? If you want to expose an entire folder through HTTP a better option would be to use the built-in [WebDAV Publishing feature](https://learn.microsoft.com/en-us/iis/install/installing-publishing-technologies/installing-and-configuring-webdav-on-iis). – Panagiotis Kanavos Apr 19 '22 at 13:35
  • Yes you're right i'm going to change my way of doing thanks for the help! – Romain Apr 19 '22 at 13:54

1 Answers1

0

I think you don't have access to the file because you don't have read permission due to its position. Move it to another folder manually and try again