问题
I want to change TNS_ADMIN property in appconfig dynamically at the runtime.
Here is the app.config;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
<bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<setting name="TNS_ADMIN" value="asd" />
</settings>
</version>
</oracle.manageddataaccess.client>
</configuration>
Currently what i am trying to do is this;
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("TNS_ADMIN", @"anylocation");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
However this adds another section.
How can i change the tnsadmin dynamically?
回答1:
Because you are using a custom section you need to do it with:
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var path = @"//oracle.manageddataaccess.client/version/settings/setting[@name='TNS_ADMIN']";
var attrs = xmlDoc.SelectSingleNode(path).Attributes["value"].Value = "some value";
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection(path);
This should work in case of default appSettings
section:
System.Configuration.Configuration cnf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cnf.AppSettings.Settings["TNS_ADMIN"].Value = "my value";
cnf.Save(ConfigurationSaveMode.Modified);
Documentation
回答2:
Your code adds a new section because you're telling it to
Instead of
config.AppSettings.Settings.Add("TNS_ADMIN", @"anylocation");
Try
config.AppSettings.Settings["TNS_ADMIN"].Value = "NewValue";
Change NewValue
to whatever you're wanting to change it to
You don't have appSettings
anywhere within your Config. This could possibly cause the error to be thrown. Won't know for sure if you do not supply the line that is throwing the error. Try wrapping your <settings>
with <appSettings>
:
<appSettings>
<version number="*">
<settings>
<setting name="TNS_ADMIN" value="asd" />
</settings>
</version>
</appSettings>
来源:https://stackoverflow.com/questions/53345670/change-app-config-programmatically-at-the-runtime