none
Cannot save Configuration File ["ConfigurationSection properties cannot be edited when locked."]

    Question

  • I have the following code Segment

    public Form1()
    {
    	InitializeComponent();
    	Configuration MyConfig;
    	ExeConfigurationFileMap MyMap = new ExeConfigurationFileMap();
    
    	MyMap.LocalUserConfigFilename = Application.UserAppDataPath + @"\" + Application.ProductName + ".xml";
    	MyMap.RoamingUserConfigFilename = Application.LocalUserAppDataPath + @"\" + Application.ProductName + ".xml"; ;
    	MyMap.ExeConfigFilename = Application.CommonAppDataPath + @"\" + Application.ProductName + ".xml"; ;
    
    //	MyConfig = ConfigurationManager.OpenMappedExeConfiguration(MyMap, System.Configuration.ConfigurationUserLevel.None );
    	MyConfig = ConfigurationManager.OpenMappedExeConfiguration(MyMap, System.Configuration.ConfigurationUserLevel.PerUserRoaming);
    //	MyConfig = ConfigurationManager.OpenMappedExeConfiguration(MyMap, System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal );
    
    	MyConfig.AppSettings.Settings.Add("Now", DateTime.Now.ToString() );
    
    	MyConfig.Save();
    }
    

    I am running for the 1st Time and trying to create the initial Config file.

    I get an exception "ConfigurationSection properties cannot be edited when locked." on the MyConfig.Save() method.  This happens with the OpenMappedExeConfiguration( ...PerUserRoaming ...PerUserRoamingAndLocal ) calls.  This does not happen with ConfigurationUserLevel.None.

    I have tried to set the Locked property of the MyConfg.AppSettings section to false, but that does not do anything.  Other posts say to update: MyConfig.AppSettings.SectionInformation.AllowExeDefinition.  However i get a similar exception when I do so.

    What do I need to do to bootstrap the user configuration files?

     

    Wednesday, October 06, 2010 1:21 PM

Answers

  •  

    Hi BrentRogers,

     

    I reproduce your issue with given code snippet, however, It doesn't sound to be an defect.

     

    In .NET Configuration hierarchy, there're four configuration levels for windows applications:

     

    LocalUser.config --> RoamingUser.config --> Application.config (appname.exe.config) --> Machine.config

     

    A configuration section, for example: appSettings, always has an allowExeDefinition attribute associated,

    The valid values for this setting are:

     

    - MachineOnly = the ConfigurationSection can be defined only in the Machine.config file.  

    - MachineToApplication = the ConfigurationSection can be defined either in the Machine.config file or in the Exe.config file in the client application directory. This is the default value. 

    - MachineToLocalUser = the ConfigurationSection can be defined in the Machine.config, in the Exe.config file in the client application directory, in the User.config file in the roaming user directory, or in the User.config file in the local user directory. 

    - MachineToRoamingUser = the ConfigurationSection can be defined in the Machine.config file, in the Exe.config file in the client application directory, or in the User.config file in the roaming user directory.

     

    this article brings more details on this topic.

     

    In machine.config, appSettings section is defined with allowExeDefinition="MachineToApplication", so we cannot write this section user's config, either roaming or local.

     

    As a workaround, you may Using Settings in C#, hope this helps.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by eryang Thursday, October 14, 2010 6:21 AM
    Thursday, October 07, 2010 8:48 AM

All replies

  •  

    Hi BrentRogers,

     

    I reproduce your issue with given code snippet, however, It doesn't sound to be an defect.

     

    In .NET Configuration hierarchy, there're four configuration levels for windows applications:

     

    LocalUser.config --> RoamingUser.config --> Application.config (appname.exe.config) --> Machine.config

     

    A configuration section, for example: appSettings, always has an allowExeDefinition attribute associated,

    The valid values for this setting are:

     

    - MachineOnly = the ConfigurationSection can be defined only in the Machine.config file.  

    - MachineToApplication = the ConfigurationSection can be defined either in the Machine.config file or in the Exe.config file in the client application directory. This is the default value. 

    - MachineToLocalUser = the ConfigurationSection can be defined in the Machine.config, in the Exe.config file in the client application directory, in the User.config file in the roaming user directory, or in the User.config file in the local user directory. 

    - MachineToRoamingUser = the ConfigurationSection can be defined in the Machine.config file, in the Exe.config file in the client application directory, or in the User.config file in the roaming user directory.

     

    this article brings more details on this topic.

     

    In machine.config, appSettings section is defined with allowExeDefinition="MachineToApplication", so we cannot write this section user's config, either roaming or local.

     

    As a workaround, you may Using Settings in C#, hope this helps.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by eryang Thursday, October 14, 2010 6:21 AM
    Thursday, October 07, 2010 8:48 AM
  •  

    We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, October 14, 2010 6:21 AM