Click here to Skip to main content
14,084,501 members
Rate this:
Please Sign up or sign in to vote.
See more:
In my program in, NOT C or C # I read my Config file for example like this:

Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections.Generic

Module Module1

    Sub LeggiConfig()

        Dim sAttr As String

        sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
        Console.WriteLine("The value of Key1: " & sAttr)
        end sub
   End Module

<?xml version="1.0" encoding="utf-8" ?>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />

    <add key="Key0" value="0" />
    <add key="Key1" value="1" />
    <add key="Key2" value="2" />

I would like to change that Key1 parameter and I tried these ways:

ConfigurationSettings.AppSettings.Set(("Key1"), "pippo" & " ")
 sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
       Console.WriteLine("The value of Key1: " & sAttr)

if I write so in the console I see the value pippo, but in the config file it still remains the initial value that was 1

if instead I add this line:

' ConfigurationSettings.AppSettings.Add(("Key1"), "pippo" & " ")

I have this error:

System.Configuration.ConfigurationErrorsException: 'Read-only configuration.'

how does this resolve?

What I have tried:

can you help Me ?

as I tried I wrote it above
Updated 20-Feb-19 7:41am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The .config file is usually sitting in the same folder as the application, under Program Files. Everything under Program Files is ReadOnly, including your .config file, to normal users. Administrators on the machine don't have that restriction, so long as the code is running with the elevated permissions ("run as Administrator").

It is a BAD IDEA to open up permissions under Program Files so users can write to the config file. You can put your additional "all users on the machine" settings for you app in it's own configuration file in a folder under C:\ProgramData and/or "user-specific" settings in it's own configuration file in the users profile folder. Exactly which folders are appropriate depend on the situation the users are expected to use the app.

Check out Environment.SpecialFolder Enum (System) | Microsoft Docs[^] for the folder locations you can get the paths for and what they are normally used for, and Environment.GetFolderPath Method (System) | Microsoft Docs[^] for how to resolve those values to folder paths.

By the way, ConfigurationSettings is now obsolete having been replaced by ConfigurationManager[^].
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Start by looking at the actual configuration file and its properties. Is the file marked as read only?
Check the folder the config file is in: who has write permission to the folder? What ID is your app running under?

If that doesn't fix it, look here: Steve Michelotti: ConfigurationErrorsException - The configuration is read only[^] - hate to say it but it's in C#, but this may help: Code Converter C# to VB and VB to C# – Telerik[^]
MadMyche 20-Feb-19 12:09pm
I've had similar issues, and have found "run as administrator" can usually remedy
OriginalGriff 20-Feb-19 12:17pm
That'll work, but it's a sledgehammer to crack a permissions nut! :laugh:
MadMyche 20-Feb-19 12:37pm
Ancient app... one time settings.... no budget to do proper fix
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Even if you have permission to edit the config file, you can't simply write to the AppSettings collection. You have to open the EXE configuration, change the settings on that instance, save that instance, and then refresh the main ConfigurationManager settings:
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;

if (settings["Key1"] == null)
    settings.Add("Key1", "pippo");
    settings["Key1"].Value = "pippo";


c# - ConfigurationManager.AppSettings - How to modify and save? - Stack Overflow[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190518.1 | Last Updated 20 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100