Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

c# - 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:PROJEKTIcrawlerWebCrawlerSuiteViaMura.WebNHibernate.config" />
    <!--<add key="NHibernateConfigPath" value="C:\_ZAGONViaMuraCurrencyAppatNHibernate.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.24SQL2008;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:PROJEKTIcrawlerWebCrawlerSuiteViaMura.WebNHibernate.config" isTransactional="true" />
      <!--<sessionFactory name="WebCrawlerFactory" factoryConfigPath="C:\_ZAGONViaMuraCurrencyAppatNHibernate.config" isTransactional="true" />-->
    </sessionFactories>
  </nhibernateSettings>

how can I programmatically edit WebCrawlerFactory? I am using

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:projectapplication.exe)

In your case, change the AppSetting by Sections:

configFile.Sections["nhibernateSettings"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...