0

I have the following code which is supposed to get the current config file, add an entry to it and then write back the file. However, this is not working (see stack trace) due to the file being locked. Is there a way to release it before doing this? Or can I do this a different way?

Code:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); // Add an Application Setting.

config.AppSettings.Settings.Add("InputDirectory" + inputdirs.Count + 1, newinputdir);
config.AppSettings.Settings.Add("OutputDirectory" + outputdirs.Count + 1, newoutputdir);

// Save the changes in App.config file.
try
{
    config.Save(ConfigurationSaveMode.Modified);
}
catch (Exception e)
{
    Console.WriteLine(e);
}
Console.WriteLine("Config update successful.");
readConfig();

Error:

System.Configuration.ConfigurationErrorsException: An error occurred executing t
he configuration section handler for appSettings. ---> System.InvalidOperationEx
ception: ConfigurationSection properties cannot be edited when locked.
   at System.Configuration.SectionInformation.VerifyIsEditable()
   at System.Configuration.MgmtConfigurationRecord.GetConfigDefinitionUpdates(Bo
olean requireUpdates, ConfigurationSaveMode saveMode, Boolean forceSaveAll, Conf
igDefinitionUpdates& definitionUpdates, ArrayList& configSourceUpdates)
   --- End of inner exception stack trace ---
   at System.Configuration.MgmtConfigurationRecord.GetConfigDefinitionUpdates(Bo
olean requireUpdates, ConfigurationSaveMode saveMode, Boolean forceSaveAll, Conf
igDefinitionUpdates& definitionUpdates, ArrayList& configSourceUpdates)
   at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, Confi
gurationSaveMode saveMode, Boolean forceUpdateAll)
   at System.Configuration.Configuration.SaveAsImpl(String filename, Configurati
onSaveMode saveMode, Boolean forceSaveAll)
   at System.Configuration.Configuration.Save(ConfigurationSaveMode saveMode)
   at EmailParserWinService.Program.Main(String[] args) in c:\Users\kylec\Docume
nts\Visual Studio 2012\Projects\EmailParserWinService\EmailParserWinService\Prog
ram.cs:line 78

I also tried manually modifying the exe configuration using the following:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); // Add an Application Setting.
XmlDocument doc = new XmlDocument();
doc.Load(@config.FilePath);

However, this creates a problem as it seems the location doesn't exist.

'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll

UPDATE:

I am now using the following:

ConfigurationSection settings = config.GetSection("EmailParserWinService.Settings");
settings.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
config.Sections.Add("GENERAL_USER_SETTINGS", settings);
config.Save();

But it is somehow a null reference. Specifically, this line: settings.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;

Somehow my settings variable (even though I defined it right above?) is coming back null. I think the problem is that I'm not pulling the correct section of code. Which section of these would contain the settings?

    [0] "system.data.dataset"   object {string}
    [1] "mscorlib"  object {string}
    [2] "system.data.sqlclient" object {string}
    [3] "connectionStrings" object {string}
    [4] "system.webServer"  object {string}
    [5] "appSettings"   object {string}
    [6] "startup"   object {string}
    [7] "runtime"   object {string}
    [8] "system.data.odbc"  object {string}
    [9] "system.data"   object {string}
    [10]    "configProtectedData"   object {string}
    [11]    "system.codedom"    object {string}
    [12]    "uri"   object {string}
    [13]    "system.runtime.remoting"   object {string}
    [14]    "satelliteassemblies"   object {string}
    [15]    "system.data.oledb" object {string}
    [16]    "assemblyBinding"   object {string}
    [17]    "system.data.oracleclient"  object {string}
    [18]    "system.windows.forms"  object {string}
    [19]    "system.diagnostics"    object {string}
    [20]    "windows"   object {string}
Kyle
  • 2,339
  • 10
  • 33
  • 67
  • http://stackoverflow.com/questions/2632806/how-to-write-to-a-user-config-file-through-configurationmanager ? – RenniePet Aug 08 '14 at 21:51
  • Thanks for the comment. Ok going through it, it does make sense. However, do you know what reference I have to add to use the UserSettings? – Kyle Aug 08 '14 at 22:00
  • Sorry, no. Maybe it isn't a .Net class name but an arbitrary user name for a class derived from some .Net class? – RenniePet Aug 08 '14 at 22:52
  • Can it be a user-defined class derived from ConfigurationSection? That makes the shown code compilable. – RenniePet Aug 08 '14 at 23:01
  • Somehow my settings variable (even though I defined it right above?) is coming back null. – Kyle Aug 08 '14 at 23:16
  • I don't really know anything about this, but can it be because your config file does not yet contain a section named "EmailParserWinService.Settings"? So you have to write/add that section to the config before you can do config.GetSection() to read it. – RenniePet Aug 08 '14 at 23:30
  • I had that thought as well, but I have a section denoted as follows: C:\TestIn1 C:\TestOut1 Should the section I"m looking for be applicationSettings then instead? – Kyle Aug 11 '14 at 13:16
  • Sorry, I don't have any experience with this. – RenniePet Aug 11 '14 at 14:45
  • Its okay, thanks for giving it a look. – Kyle Aug 11 '14 at 15:16

0 Answers0