Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

like image 566
senzacionale Avatar asked Dec 15 '11 15:12

senzacionale


People also ask

What is the use of ConfigurationManager in C#?

ConfigurationManager is the class which helps to read data from configurations. Provides access to configuration files for client applications. To use the ConfigurationManager class, your project must reference the System. Configuration assembly.

Where is app config file in C#?

A: When you compile an application, its app. config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a "text.exe. config" in your bin directory.


2 Answers

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"]
like image 55
Freddy Avatar answered Oct 07 '22 06:10

Freddy


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

like image 1
BaTTy.Koda Avatar answered Oct 07 '22 06:10

BaTTy.Koda