我有以下代码,它应该获取当前配置文件,向其中添加一个条目然后写回文件。但是,由于文件被锁定,这不起作用(请参阅堆栈跟踪)。有没有办法在这之前释放它?或者我可以用不同的方式做到这一点吗?
代码:
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();
错误:
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
我还尝试使用以下方法手动修改exe配置:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); // Add an Application Setting.
XmlDocument doc = new XmlDocument();
doc.Load(@config.FilePath);
然而,这会产生一个问题,因为它似乎不存在。
' System.IO.DirectoryNotFoundException'发生在System.Xml.dll
中更新:
我现在正在使用以下内容:
ConfigurationSection settings = config.GetSection("EmailParserWinService.Settings");
settings.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
config.Sections.Add("GENERAL_USER_SETTINGS", settings);
config.Save();
但它在某种程度上是一个空引用。具体来说,这一行: settings.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
不知怎的,我的设置变量(即使我在上面定义了它?)回来了。我认为问题在于我没有提取正确的代码部分。其中哪一部分包含设置?
[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}