Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何打开格式化为.NET配置文件的文件以获取System.Configuration.Configuration对象_Asp.net_.net_Configuration - Fatal编程技术网

Asp.net 如何打开格式化为.NET配置文件的文件以获取System.Configuration.Configuration对象

Asp.net 如何打开格式化为.NET配置文件的文件以获取System.Configuration.Configuration对象,asp.net,.net,configuration,Asp.net,.net,Configuration,我在一个ASP.NET应用程序中,我想打开一个格式类似于web.config的文件或一个可将其链接到的配置文件(appSettings、pages等)并获取appSettings Dim filePath As String = HostingEnvironment.MapPath("~/developer.config") If (File.Exists(filePath)) Then Dim map As New WebConfigurationFileMap() map.Mac

我在一个ASP.NET应用程序中,我想打开一个格式类似于web.config的文件或一个可将其链接到的配置文件(appSettings、pages等)并获取appSettings

Dim filePath As String = HostingEnvironment.MapPath("~/developer.config")
If (File.Exists(filePath)) Then
   Dim map As New WebConfigurationFileMap()
   map.MachineConfigFilename = filePath
   Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(map, "/")
   Dim developerAppSettings As AppSettingsSection = config.AppSettings
   Return developerAppSettings
Else
   Return Nothing
End If
我想将
developerAppSettings
设置到我的developer.config文件的appSettings部分,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
    <add key="CustomerLogosFolder" value="C:\code\ExactBidTFS\RIMS\Development\CustomerLogos" />
</appSettings>

显然,我没有正确使用OpenMappedWebConfiguration(),因为它给了我ArgumentOutOfRange异常(它似乎是第二个“/”参数)


这可能吗?我也尝试过OpenWebConfiguration(),但在这种情况下,对于file path参数的用途似乎有些混淆。我的实验表明,文件路径只是一个包含web.config的虚拟目录,而不是指定我自己的developer.config文件。

我在尝试从控制台应用程序打开web应用程序的配置文件时看到了这个问题。我还看到了帮助我的帖子

我知道这是一个老问题,但我想我还是把我的解决方案贴在这里吧

我写了以下代码片段,对我很有用:

Dim map As New ExeConfigurationFileMap()
Dim filePath As String = "c:\temp\developer.config"
map.ExeConfigFilename = filePath
Dim configFile As Configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)
If configFile IsNot Nothing Then
  Dim myAppSection As AppSettingsSection = TryCast(configFile.GetSection("appSettings"), AppSettingsSection)
  Debug.Print(myAppSection.Settings("CustomerLogosFolder").Value)
End If
我还对xml进行了如下修改,使其与其他配置文件类似:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="CustomerLogosFolder" value="C:\code\ExactBidTFS\RIMS\Development\CustomerLogos" />
    </appSettings>
</configuration>

在执行结束时,我在即时窗口中看到“C:\code\ExactBidTFS\RIMS\Development\CustomerLogos”