14

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);

Cœur
  • 37,241
  • 25
  • 195
  • 267
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • you can do this using XPATH, I do not have an example here but I have done this using XPATH to update sections in my web.config file do a Google search for updating Configuration Sections using XPATH C# – MethodMan Dec 15 '11 at 16:11
  • something like this: http://patelshailesh.com/index.php/update-web-config-programmatically – senzacionale Dec 15 '11 at 16:38
  • Take a look at my example I have posted a correct repsonse for a previous user.. [changeConfigFile Runtime](http://stackoverflow.com/questions/8130085/opening-the-machine-base-web-config-64bit-through-code/8142160#8142160) – MethodMan Dec 15 '11 at 17:43

2 Answers2

31

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"]
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Freddy
  • 319
  • 3
  • 4
1

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

BaTTy.Koda
  • 173
  • 6
  • It actually looks like the ProjectBase.Data.OpenSessionInViewSection is unrelated to NHibernte. That being said, I don't think that it would interfere with NHibernate. – BaTTy.Koda Dec 15 '11 at 22:56