我正在一个项目上,该项目需要我为我编写的Windows服务(用ASP.NET编写)修改.config文件。需要从我正在开发的管理网站上进行更改。当用户选择Web应用程序上的“更新”按钮时,将执行以下代码:
Protected Sub ModifyAppConfig()
Try
Dim configFile = "C:\App\App.exe.config"
Dim configFileMap As New ExeConfigurationFileMap
configFileMap.ExeConfigFilename = configFile
Dim config As System.Configuration.Configuration
config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
config.AppSettings.Settings("LogDirectory").Value = txtLogDirectory.Text '* EXCEPTION IS THROWN HERE *
config.Save()
Catch ex As Exception
End Try
End Sub
当应用程序尝试访问“ LogDirectory” AppSettings索引时,将引发异常。 Object reference not set to an instance of an object.
在调试期间,AppSettings.Count = 0
。为什么没有导入任何AppSettings?
我知道它正确打开了文件,因为有ConnectionString数据。下面是配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="conn" connectionString="Data Source=DBASE;Initial Catalog=NGDevl;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<appSettings>
<add key="LogDirectory" value="C:\CTemp\"/>
</appSettings>
</configuration>
如何访问和修改AppSettings(“ LogDirectory”)值?
答案 0 :(得分:0)
坦率地说,您的程序没有任何问题。
我创建了一个VB.Net/控制台模式MCVE,可以帮助您进行故障排除
Module1.vb
Imports System.Configuration
'
' EXAMPLE OUTPUT:
' Reading config file(test.exe.config)...
' config.AppSettings.Settings("LogDirectory") : C : \CTemp\
' connectionStrings: <connectionStrings>
' <add name = "conn" connectionString="Data Source=DBASE;Initial Catalog=NGDevl;
' Integrated Security=True" providerName="System.Data.SqlClient"/>
' </connectionStrings>
' appSettings: <appSettings>
' <add key = "LogDirectory" value="C:\CTemp\"/>
' </appSettings>
' startup: <startup>
' <supportedRuntime version = "v4.0" sku=".NETFramework,Version=v4.5"/>
' </startup>
' 3 sections were found.
'
Module Module1
Sub Main()
' Get filename
Dim args() As String = System.Environment.GetCommandLineArgs()
If args.Length <> 2 Then
Console.WriteLine("USAGE: ReadAppConfig <\path\to\app.config>")
Return
End If
Dim configFilePath As String = args(1)
' Open config file
Try
Console.WriteLine("Reading config file(" & configFilePath & ")...")
Dim configFileMap As New ExeConfigurationFileMap
configFileMap.ExeConfigFilename = configFilePath
Dim config As System.Configuration.Configuration
config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
' Try to read AppSettings["LogDirectory"]
Dim obj = config.AppSettings.Settings("LogDirectory")
If IsNothing(obj) Then
Console.WriteLine("config.AppSettings.Settings(""LogDirectory""): NULL")
Else
Console.WriteLine("config.AppSettings.Settings(""LogDirectory""): " & obj.Value)
End If
' Dump all Sections
Dim ct As Integer = 0
For Each section As ConfigurationSection In config.Sections
Dim xml = section.SectionInformation.GetRawXml
If Not xml = Nothing Then
Console.WriteLine(section.SectionInformation.Name & ": " & xml)
ct = ct + 1
End If
Next
Console.WriteLine(ct & " sections were found.")
Catch ex As Exception
System.Console.WriteLine("ERROR: " & ex.Message)
End Try
End Sub
End Module
test.exe.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="conn" connectionString="Data Source=DBASE;Initial Catalog=NGDevl;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<appSettings>
<add key="LogDirectory" value="C:\CTemp\"/>
</appSettings>
</configuration>
项目> test.exe.config::我将“复制到输出”属性设置为“如果较新则复制”
项目>属性>调试:我将“命令行参数”设置为“ test.exe.config”
'希望有帮助...