tnw
tnw

Reputation: 13877

Access web.config settings from external program

I have an application residing in a virtual directory. It has the following setting in its Web.config:

<appSettings>
    <add key="SomePath" value="C:\Somepath\whatever"/>
    ........other settings.........
</appSettings>

I have an executable that runs externally to the app that needs to read this value:

System.Configuration.Configuration config =  
     WebConfigurationManager.OpenWebConfiguration("/MyApplicationVirtualDirectory") 
     as System.Configuration.Configuration;
string path = config.AppSettings.Settings["SomePath"].Value;

This throws a compilation error:

'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level

Which I guess makes sense because web.configs in virtual directories contain some sensitive info like connection strings, etc but I wanted to check and make sure that I'm not doing anything wrong and that my understanding is correct. If not, how can I access this setting from my external executable?

Upvotes: 0

Views: 324

Answers (1)

Mr. B
Mr. B

Reputation: 2975

This is sort of a round-abound way of solving the problem, but its the way I've done it in the past. You can simply make a page in your Web Application that gives the setting you want back, and then use an HttpWebRequest to access that page in your executable.

Otherwise its a permission issue. You can mess with the permissions on the Web.config file or try running the executable as Administrator.

Upvotes: 1

Related Questions