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.

2
  • 1
    Think that could help : stackoverflow.com/questions/505566/… Commented Jun 5, 2014 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. Commented Jun 5, 2014 at 11:32

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
And you're sure the name of the connectionstring does exists in the file you specified ?
config.ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString worked, though not sure why.
Glad to see that it helped you out. Please consider marking the answer as accepted is this answer was your solution.
msdn.microsoft.com/en-us/library/vstudio/… shows the ConnectionStrings.ConnectionStrings syntax is correct
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.

Comments

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.

Comments

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.

2 Comments

exactly what I wanted to avoid!
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.