We help IT Professionals succeed at work.

Determine name of external configuration reference in web.config file.

ChetOS82
ChetOS82 asked
on
690 Views
Last Modified: 2012-08-13
In my web.config file, I am referencing an external file for my appSettings:

<appSettings file="myAppSettings.config" />

Is it possible, without parsing the web.config file as an XML document, to determine the value of the "file" attribute?  That is to say, does the .NET framework expose this as a property (in the System.Configuration.ConfigurationManager namespace perhaps)?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You could use ConfigurationManager.AppSettings, but that entry doesn't have a key to use Configuration.AppSettings[key].

Bob
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Example:

<appSettings>
     <add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>

Bob

Author

Commented:
What I am really looking for is the value of the "file" attribute.  I am referencing an external config file.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Try this:

   Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   AppSettingsSection settings = (AppSettingsSection)config.GetSection("appSettings");

   string file = settings.File;

Bob

Author

Commented:
I get an error on the first line: "exePath must be specified when not running inside a stand alone exe"

Changing "ConfigurationUserLevel.None" to the absolute path of the web.config file prevents the error, but then settings.File returns a blank string.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Works, thanks.  I have never used the System.Web.Configuration namespace.

Any idea if there are any file permission issues (it works on my machine, any chance it won't work on the production server)?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Usually you should have rights to the root folder, even in a production environment.  I can't guarantee 100% that you won't have problems.

Bob
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.