Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my program in VB.net, 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" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>

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


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

ConfigurationSettings.AppSettings.Set(("Key1"), "pippo" & " ")
My.Settings.Save()
 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
Posted
Updated 20-Feb-19 7:41am

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[^].
 
Share this answer
 
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[^]
 
Share this answer
 
Comments
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
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:
C#
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;

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

configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900