13

I am writing a test platform for some semi-automated testing using a Console application, but I need to get the connection string from the project I am testing. I don't want to reference the other application directly or otherwise have an accessor in the project I'm testing.

What I've managed to do so far is create a link to the other project's Web.config file in my TestUtility project, and I've set it to Copy if newer. It's the only Web.config in my test project's root folder, but WebConfigurationManager.OpenWebConfiguration(null) seems to be opening some OTHER Web.config, as the only connection string in it refers to .\SQLEXPRESS (not in any file in my solution, my path would be .\sql2008 in this configuration - which varies).

Any hints or tips as to how to access that config section from another project?

(Yay first question)

Rob G
  • 3,496
  • 1
  • 20
  • 29
  • Is it possible to specify a relative path to your web.config for `OpenWebConfiguration` instead of passing null? – Conrad Clark Apr 05 '13 at 16:24
  • Yes, I have tried that as well to no avail. – Rob G Apr 05 '13 at 16:25
  • `RobG` you can do this take a look at the following `http://msdn.microsoft.com/en-us/library/ms224437.aspx` – MethodMan Apr 05 '13 at 16:25
  • Check if any of these answers can help you: http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location – Conrad Clark Apr 05 '13 at 16:28
  • None of those solutions work for Web.configs, it seems. Even when I use `OpenMappedWebConfig` from the `WebConfigurationManager` it does not work correctly and I get the same, wrong configuration. Is there somewhere that I can specify the configuration file it's using so it doesn't do whatever it's doing with a default config? – Rob G Apr 05 '13 at 16:38
  • I'm sure I did that more than once - try skipping the web... open it as any other config. `OpenMappedExeConfiguration` or something – NSGaga-mostly-inactive Apr 05 '13 at 16:41
  • Even though it opens a configuration using OpenExeConfiguration, I still get the same result for the connectionString: `.\SQLEXPRESS` – Rob G Apr 05 '13 at 16:54

2 Answers2

25

Better late than never:

var filePath = @"D:\PathToConfig\Web.config";
var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

I can't take the credit for this one though, I found it here!

animuson
  • 53,861
  • 28
  • 137
  • 147
Gabriel
  • 1,572
  • 1
  • 17
  • 26
3

The solution I found was to open it as an XDocument and parse it manually:

        XDocument xdoc = XDocument.Load("Test/Web.config");

        var path = xdoc.Element("configuration").Element("connectionStrings").Element("add").Attribute("connectionString").Value;

Though if you had multiple connection strings, you would want to use the .Elements("add") method on the connectionStrings element and iterate over the various strings.

Rob G
  • 3,496
  • 1
  • 20
  • 29