Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SettingsObj
- {
- [String] $location
- $settings
- SettingsObj ([String]$location)
- {
- $this.location = $location
- $this.updateSettings();
- }
- updateSettings() {
- #If there are already settings in the specified location, load them
- if([System.IO.File]::Exists($this.location)){
- $this.settings = Import-Clixml $this.location
- } else {
- #Otherwise, create a new, empty, settings object
- $this.settings = New-Object 'System.Collections.Generic.Dictionary[String,String]'
- }
- }
- [String] getSetting([String]$setting) {
- $this.updateSettings();
- return $this.settings[$setting]
- }
- [bool] hasSetting([String]$setting) {
- $this.updateSettings();
- return $this.settings.ContainsKey($setting)
- }
- setSetting([String]$setting, [String]$newVal) {
- $this.updateSettings();
- if($this.settings.ContainsKey($setting)){
- this.settings[$setting] = $newVal;
- } else {
- $this.settings.Add($setting, $newVal)
- }
- #Whenever settings are added/changed, export them so they are on-disk
- Export-Clixml -InputObject $this.settings -Path $this.location
- }
- removeSetting([String]$setting) {
- $this.updateSettings();
- if($this.settings.ContainsKey($setting)){
- $this.settings.Remove($setting)
- #Whenever settings are removed, export them so they are on-disk
- Export-Clixml -InputObject $this.settings -Path $this.location
- }
- }
- printSettings() {
- Write-Host $($this.settings | Out-String)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement