M4N
M4N

Reputation: 96561

Find the name of the current config file

Is there a way to find the name (and path) of the current application's config file from inside a class library?

E.g. in a web app this would be web.config, in a windows app or service this would be myapp.exe.config.

Upvotes: 8

Views: 3062

Answers (2)

Andrew Hare
Andrew Hare

Reputation: 351516

Use Configuration.FilePath:

Gets the physical path to the configuration file represented by this Configuration object.

Upvotes: 0

SLaks
SLaks

Reputation: 887453

Try this:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

If that doesn't work, add references to System.Web and System.Configuration, then try this:

if(HttpRuntime.AppDomainAppVirtualPath  == null) 
    return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
else
    return VirtualPathUtility.ToAbsolute("~/Web.config");

Upvotes: 16

Related Questions