C# 从外部程序访问web.config设置

C# 从外部程序访问web.config设置,c#,web-config,C#,Web Config,我有一个驻留在虚拟目录中的应用程序。它的Web.config中有以下设置: <appSettings> <add key="SomePath" value="C:\Somepath\whatever"/> ........other settings......... </appSettings> 这会引发编译错误: “System.Configuration.ConfigurationElement.this[System.Configur

我有一个驻留在虚拟目录中的应用程序。它的
Web.config
中有以下设置:

<appSettings>
    <add key="SomePath" value="C:\Somepath\whatever"/>
    ........other settings.........
</appSettings>
这会引发编译错误:

“System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]”由于其保护级别而无法访问


我想这是有道理的,因为虚拟目录中的web.configs包含一些敏感信息,如连接字符串等,但我想检查并确保我没有做错任何事情,并且我的理解是正确的。如果没有,我如何从外部可执行文件访问此设置?

这是一种解决问题的方法,但这是我过去的方法。您只需在Web应用程序中创建一个页面,提供您想要的设置,然后使用HttpWebRequest在可执行文件中访问该页面


否则就是许可问题。您可以修改Web.config文件上的权限,或者尝试以管理员身份运行可执行文件。

Hmph。现在,我没有得到不可访问性错误,但得到了一个空引用。
AppSettings.Settings[“SomePath”]
返回并尝试进行更多挖掘。我认为我描述的第一个解决方案是最好的选择。我以前多次使用过这个问题的答案,没有任何问题:也许你应该寻找另一种方式来共享配置。
System.Configuration.Configuration config =  
     WebConfigurationManager.OpenWebConfiguration("/MyApplicationVirtualDirectory") 
     as System.Configuration.Configuration;
string path = config.AppSettings.Settings["SomePath"].Value;