How To Set Appsettings.Json Readonly On .NetCore

Viewed 223

I have a project template on visual studio for .net core 3.1 and I want to set appsettings.json and appsettings.Development readonly, appsettings.local read/write for developers.

Is it possible to to?

1 Answers

You could open the project root folder via File Explorer, then set the appsettings.json file Read-only, then when you update the file content programmatically, it will show an exception to prevent update.

enter image description here

Note

By using the above method, when you update the appsettings.json file via visual studio, it will show this prompt:

enter image description here

If you choose the "Overwride" button, you have to re-set the appsettings.json file Read-only.

Besides, when using System.IO.File class to write the value in the appsettings.json file, it will throw an exception (because the file is read-only). So, you have to use the Try-Catch statement to catch the exception.

Related