我正在开发一个项目,要求我修改我写的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”)值?
坦率地说,我认为你的程序没有任何问题。
我已经创建了一个VB.Net/Console模式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>
'希望有帮助......