I am working on a project that requires me to modify the .config file for a Windows service (written in ASP.NET) I wrote. The changes need to be made from an administrative website I am developing. When the user selects the 'UPDATE' button on the web app, the code below is executed:
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
When the application is trying to access "LogDirectory" AppSettings index, an exception is thrown. Object reference not set to an instance of an object.
During debugging, the AppSettings.Count = 0
. How come none of the AppSettings are imported?
I know that it is correctly opening the file because there are ConnectionString data. Below is the config File:
<?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>
How can I access and modify the AppSettings("LogDirectory") value?