0

I am writing a command line utility which will sit in a web applications's bin folder, and needs to read the connection string of that web application, in order to perform some maintenance tasks on that database.

I have read plenty of examples of how to use ConfigurationManager.OpenExeConfiguration to read settings from the current application's config file. But this doesn't appear to apply in my case, as I need to read another application's config file.

One way to go is to use XDocument.Load which will read the xml file and allow me to manipulate it using LinqToXml. If I have to learn this method I will do, but it will take a little tinkering time to get right.

Is there a way I can take advantage of some part of ConfigurationManager to do this?

Both System.Configuration and System.Xml.Linq are large, and I'm starting from zero - so any hints on how to accomplish this with minimum fuss would be appreciated.

Greg Woods
  • 2,697
  • 2
  • 26
  • 18
  • 1
    Think that could help : http://stackoverflow.com/questions/505566/loading-custom-configuration-files – Raphaël Althaus Jun 05 '14 at 11:06
  • Thanks @RaphaëlAlthaus that question is exactly what I needed. My question is almost a duplicate except for my additional 'get connection string' requirement, which is no doubt covered in other questions. – Greg Woods Jun 05 '14 at 11:32

4 Answers4

3

Normally, the ConfigurationManager allows you to specify the path for a file.

See the following code:

private const string configFile = @"C:\Directory\SubDirectory\file.config";

public static string GetConnectionString()        
{ 
    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configFile }; 

    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

    return config.ConnectionStrings.ConnectionStrings["MYConnectionString"].ConnectionString;
}

Note: Untested and written out of my head.

Greg Woods
  • 2,697
  • 2
  • 26
  • 18
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • This, and the comment by @RaphaëlAlthaus set me on the right path - although the connection string bit doesn't work for me. The last line gives an "inaccessible due to its protection level" error. – Greg Woods Jun 05 '14 at 11:34
  • And you're sure the name of the connectionstring does exists in the file you specified ? – Complexity Jun 05 '14 at 11:39
  • config.ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString worked, though not sure why. – Greg Woods Jun 05 '14 at 11:39
  • Glad to see that it helped you out. Please consider marking the answer as accepted is this answer was your solution. – Complexity Jun 05 '14 at 11:40
  • http://msdn.microsoft.com/en-us/library/vstudio/ms178411(v=vs.100).aspx shows the ConnectionStrings.ConnectionStrings syntax is correct – Greg Woods Jun 05 '14 at 11:41
0

Here is the solution and explanation : Relocating app.config file to a custom path The problem is that using AppDomain is a bit more difficult than learning Linq2Xml. My suggestion is to just work onthe file as an xml. Beside Linq2Xml, there are additional methods to process an xml file : XmlDocument and XPath. If you only need a few elements from the config file, I suggest using XPath, otherwise Linq2Xml is the way to go.

Community
  • 1
  • 1
Alex Barac
  • 632
  • 4
  • 12
0

By default, there is only 1 ".config" file for a running application. It is the ".config" file associated with the EXE that started the program.You should probably copy the config values from Another app/web .config to running application.

Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

Please refer to this link. You will find out how to use XDocument to load the file, Linq-to-XML or XPath to find your strings.

Community
  • 1
  • 1
martin
  • 289
  • 1
  • 6
  • exactly what I wanted to avoid! – Greg Woods Jun 05 '14 at 11:34
  • 1
    I know, but sometimes we have to use solutions that we're not thrilled about. However, your case appears to be simple, so two lines of code shouldn't present a massive time investment. As Alex Barac already suggested, this is the way to go if you don't want an overkill. I personally would not go through the relocation process because I didn't see the need for it based on your question (the cmdline app resides inside Bin folder) – martin Jun 05 '14 at 11:41