Freigeben über


SYSK 73: On the Importance of allowExeDefinition Attribute

When you create an application scoped setting, the allowExeDefinition attribute on the <section> element is omitted.  For user setting, it’s added by VS and set to value “MachineToLocalUser”. 

 

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 MachineToLocalUser file, 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.  

 

If you don’t have allowExeDefinition (it’s missing) or it’s set to MachineToApplication or to MachineToRoamingUser, and you try to change user settings at run time

        WindowsApplication1.Properties.Settings.Default.MySetting = textBox1.Text;

     WindowsApplication1.Properties.Settings.Default.Save();

you’ll get “System.InvalidOperationException: ConfigurationSection properties cannot be edited when locked” exception:

If you have allowExeDefinition set to MachineOnly, you’ll get a different exception --

"It is an error to use a section registered as allowExeDefinition='MachineOnly' beyond machine.config”.

 

If you set allowExeDefinition to MachineToLocalUser, the code above will run without any errors.

 

Interestingly, if you remove or set the allowExeDefinition attribute to values other than MachineToLocalUser after the user.config file was created, you will no longer get the “System.InvalidOperationException: ConfigurationSection properties cannot be edited when locked” exception. Instead, you’ll get "It is an error to use a section registered as allowExeDefinition='YourSettingHere' beyond machine.config”

Comments

  • Anonymous
    December 31, 2007
    Thank you for lucidly explaining this. I've seen 15 different answers to the "Why do I get 'ConfigurationSection properties cannot be edited when locked' when I call Save()" question, and your's is the first to be spot on!

  • Anonymous
    March 25, 2008
    You also need to set this property when adding a new section to your config file. For example, if you want your app to be able to start up (the first time) with no config file, you can check for a null return when you try to load a custom section; if the section is null, create a new custom section (use "new"), set its LockItem property to False, set its SectionInformation's ForceSave to true and AllocExeDefinition to ...MachineToLocalUser, and then add it with config.Sections.Add.

  • Anonymous
    May 16, 2008
    Great explanation of which errors happen when, if only I had known this sooner :p.

  • Anonymous
    June 03, 2008
    I concur with the sentiments of paulwh.

  • Anonymous
    January 28, 2009
    Kudos, I can only agree with previous commenters. After too much time searching for a solution I came across your post and realised instantly what I was missing. Thanks!

  • Anonymous
    February 06, 2009
    The comment has been removed

  • Anonymous
    September 07, 2009
    thank you for this explanation. actually this is also valid for CAB.