1

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?

Sebastian 506563
  • 5,093
  • 2
  • 26
  • 49
john true
  • 255
  • 3
  • 20

2 Answers2

2

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

Sebastian 506563
  • 5,093
  • 2
  • 26
  • 49
  • System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Configuration.KeyValueConfigurationCollection.this[string].get returned null. – john true Nov 16 '18 at 21:52
  • @johntrue , It would be helpful if you supplied what line is causing the error :) – Symon Nov 16 '18 at 21:56
  • what is your project structure? And where app.config is. – Sebastian 506563 Nov 16 '18 at 21:56
  • @Sebastian506563 app.config is in the same location with .exe. The setting should be made under of oracle.manageddataaccess.client – john true Nov 16 '18 at 21:57
  • 1
    If this is the correct way to do it- then this should be marked as answer instead. (I have reached my knowledge threshold on the subject so I cannot provide further input). – Symon Nov 16 '18 at 22:40
0

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>
Sebastian 506563
  • 5,093
  • 2
  • 26
  • 49
Symon
  • 947
  • 1
  • 8
  • 27
  • System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Configuration.KeyValueConfigurationCollection.this[string].get returned null. – john true Nov 16 '18 at 21:52
  • I have tested your exact code (added a section to a config to match yours) and cannot recreate your error. What line is erroring out for you? – Symon Nov 16 '18 at 22:02
  • I added fully app.config. could you try again? – john true Nov 16 '18 at 22:05
  • it still throws the same error at line; config.AppSettings.Settings["TNS_ADMIN"].Value = "NewValue"; – john true Nov 16 '18 at 22:16
  • 1
    @johntrue , Without seeing more code, I'm afraid there is not able more advice to give (on my end). The error is suggesting you're using a [null value](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) somewhere. [Here](https://stackoverflow.com/questions/6606626/configurationmanager-appsettings-in-returning-null) is another SO answer that dealt with that issue. – Symon Nov 16 '18 at 22:25
  • `AppSettings` is used for appSettings section. – Sebastian 506563 Nov 16 '18 at 22:36