senzacionale
senzacionale

Reputation: 20916

update app.config file programmatically with ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

update app.config file programmatically with

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

this is my xml

<configuration>
  <configSections>
    <section name="nhibernateSettings" type="ProjectBase.Data.OpenSessionInViewSection, ProjectBase.Data" />
  </configSections>
  <appSettings>
    <add key="NHibernateConfigPath" value="D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\NHibernate.config" />
    <!--<add key="NHibernateConfigPath" value="C:\_ZAGON\ViaMura\CurrencyApp\at\NHibernate.config" />-->
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <connectionStrings>
    <add name="connectionString" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Viamura_at;Data Source=.\SQL2008" providerName="System.Data.SqlClient" />
    <!--<add name="connectionString" connectionString="server=193.37.152.24\SQL2008;User Id=DBUser;password=Lualah8991;database=Viamura_at" providerName="System.Data.SqlClient" />-->
  </connectionStrings>
  <nhibernateSettings>
    <!-- List every session factory that will be needed; transaction management and closing sessions 
          will be managed with the open-session-in-view module -->
    <sessionFactories>
      <clearFactories />
      <sessionFactory name="WebCrawlerFactory" factoryConfigPath="D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\NHibernate.config" isTransactional="true" />
      <!--<sessionFactory name="WebCrawlerFactory" factoryConfigPath="C:\_ZAGON\ViaMura\CurrencyApp\at\NHibernate.config" isTransactional="true" />-->
    </sessionFactories>
  </nhibernateSettings>

how can I programmatically edit WebCrawlerFactory? I am using

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Upvotes: 14

Views: 59361

Answers (2)

Freddy
Freddy

Reputation: 319

You can use the following code:

private void UpdateConfig(string key, string value, string fileName)
{
    var configFile = ConfigurationManager.OpenExeConfiguration(fileName);
    configFile.AppSettings.Settings[key].Value = value;

    configFile.Save();
}

Where: fileName is the full path + application name (c:\project\application.exe)

In your case, change the AppSetting by Sections:

configFile.Sections["nhibernateSettings"]

Upvotes: 31

BaTTy.Koda
BaTTy.Koda

Reputation: 173

The ProjectBase.Data.OpenSessionInViewSection indicates that there is already a custom config section defined that will allow access to the config settings. It may, however be protected or internal to NHibernate.

See if you can reference that class to access the settings.

You could also create a custom configuration section yourself, however it would cause NHibernate to be improperly configured since it would not be able to load the config section properly.

see How to: Create Custom Configuration Sections Using ConfigurationSection

Upvotes: 1

Related Questions