Skip to content

Instantly share code, notes, and snippets.

@Ari24-cb24
Created May 17, 2024 13:47
Show Gist options
  • Save Ari24-cb24/74c2c41781c6b64b73f0d0119e07c227 to your computer and use it in GitHub Desktop.
Save Ari24-cb24/74c2c41781c6b64b73f0d0119e07c227 to your computer and use it in GitHub Desktop.
Config Handler for vbnet 2.4+. System.Configuration must be linked
Imports System.Configuration
Imports System.Reflection
Public Class AppConfig
Private _config As Configuration
Private _settings As AppSettingsSection
Public Function GetProperty(propertyName As String) As String
If Not HasProperty(propertyName) Then
Return Nothing
End If
Return _settings.Settings.Item(propertyName).Value
End Function
Public Function GetProperty(propertyName As String, defaultValue As String) As String
If Not HasProperty(propertyName) Then
SetProperty(propertyName, defaultValue)
Return defaultValue
End If
Return _settings.Settings.Item(propertyName).Value
End Function
Public Sub SetProperty(propertyName As String, propertyValue As String)
If HasProperty(propertyName) Then
_settings.Settings.Item(propertyName).Value = propertyValue
Else
_settings.Settings.Add(propertyName, propertyValue)
End If
End Sub
Public Function HasProperty(propertyName As String)
Return _settings.Settings.AllKeys.Contains(propertyName)
End Function
Public Sub New()
_config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location)
_settings = _config.AppSettings
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
_config.Save(ConfigurationSaveMode.Modified)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment