1

How do I initialize a connection string from a remote file inside my Console Application in C# :

I am currently doing this for AppSettings to load from a remote location:

string runtimeAppSettingsconfigfile = "c:\Somepath\StaticAppSettings.config"

System.Configuration.Configuration config
    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = runtimeAppSettingsconfigfile;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

How do I achieve the same for Connection strings config file i.e. load the connection string file remotely ?

EDIT :

What I have tried so far is :

string runtimeConnectionStringConfigfile = "c:\somepath\StaticConnectionStrings.config"
Configuration config =                          ConfigurationManager.OpenExeConfiguration(runtimeConnectionStringConfigfile);

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");

Connectionstring file sample:

<connectionStrings>
    <add name="mydb" connectionString="Data Source=PrimaryDB;Initial Catalog=test;Persist Security Info=True;User ID=testuser;Password=test; Asynchronous Processing=true" providerName="System.Data.SqlClient" />
</connectionStrings>
Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109
  • What do you want to load from? Sounds like it's something other than app.config. – sircodesalot Feb 12 '13 at 17:30
  • can you post a sample of you Connection Strings file? if it's the same from where you read appSettings have you tried putting ConnectionStrings where you have appSettings in your sample? – 537mfb Feb 12 '13 at 17:33
  • @537mfb I tried that, but if you check the code it specifically says : config.AppSettings.File - and If I use it again it just overrides the previous section i.e. my appsettings.config file with my connectionstrings.config file – Murtaza Mandvi Feb 12 '13 at 17:38
  • @GalacticCowboy Updated question, please check the question again.Thanks! – Murtaza Mandvi Feb 12 '13 at 17:41
  • @MurtazaMandvi - Course it gets overriden - you're calling config.save on both. Have you tried Oliver's answer from http://stackoverflow.com/questions/505566/loading-custom-configuration-files ? – 537mfb Feb 12 '13 at 18:08

0 Answers0