'i00 File Settings
'©i00 Productions All rights reserved
'Created by Kris Bennett
'----------------------------------------------------------------------------------------------------
'All property in this file is and remains the property of i00 Productions, regardless of its usage,
'unless stated otherwise in writing from i00 Productions.
'
'i00 is not and shall not be held accountable for any damages directly or indirectly caused by the
'use or miss-use of this product. This product is only a component and thus is intended to be used
'as part of other software, it is not a complete software package, thus i00 Productions is not
'responsible for any legal ramifications that software using this product breaches.
Imports System.Xml.Serialization
Imports System.IO
Imports System.Text.RegularExpressions
Namespace Settings
Namespace AppSettings
<Serializable()> _
Public Class clsSettings
Public Setting As New Generic.List(Of clsSetting)
<Serializable()> _
Public Class clsSetting
Public Key As String
Public Value As Object
Public Desc As String
End Class
End Class
<Serializable()> _
Public Class sFont
Public FontName As String
Public Size As Single
Public Style As Integer
End Class
<Serializable()> _
Public Class sColor
Public A As Byte
Public R As Byte
Public G As Byte
Public B As Byte
End Class
End Namespace
Public Class XMLSettings
Private WithEvents SettingsBase As System.Configuration.ApplicationSettingsBase
Private File As String
Public Sub New(ByVal SettingsBase As System.Configuration.ApplicationSettingsBase, ByVal File As String)
Me.SettingsBase = SettingsBase
Me.File = File
userOverride_SettingsLoaded(Nothing, Nothing)
End Sub
Public Function GetDictionaryOfSettings() As Dictionary(Of String, Object)
Dim Dict As New Dictionary(Of String, Object)
For Each appProperty As System.Configuration.SettingsProperty In SettingsBase.Properties
Dict.Add(appProperty.Name, SettingsBase.Item(appProperty.Name))
Next
Return Dict
End Function
Private Function GetSettingsFile() As String
Return System.IO.Path.GetFullPath(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, File))
End Function
Private Sub MySettings_SettingChanging(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs) Handles SettingsBase.SettingChanging
If SettingsHaveLoaded = False Then
userOverride_SettingsLoaded(Nothing, Nothing)
End If
End Sub
Dim SettingsHaveLoaded As Boolean
Public Sub userOverride_SettingsLoaded(ByVal sender As Object, _
ByVal e As System.Configuration.SettingsLoadedEventArgs) _
Handles SettingsBase.SettingsLoaded
SettingsHaveLoaded = True
If FileIO.FileSystem.FileExists(GetSettingsFile) Then
Try
Dim deser As XmlSerializer = New XmlSerializer(GetType(AppSettings.clsSettings), New Type() {GetType(Color), GetType(System.Drawing.Size)})
Dim streamrd As New StreamReader(GetSettingsFile)
Dim clsSettings As AppSettings.clsSettings
clsSettings = deser.Deserialize(streamrd)
streamrd.Close()
For Each Item As AppSettings.clsSettings.clsSetting In clsSettings.Setting
Try
If TypeOf Item.Value Is System.Array Then
If TypeOf Item.Value(0) Is System.Xml.XmlAttribute Then
Dim ValueType As System.Xml.XmlAttribute = Item.Value(0)
Select Case ValueType.Value
Case "sColor"
Dim sColor As Color = Color.FromArgb(Item.Value(1).innertext, Item.Value(2).innertext, Item.Value(3).innertext, Item.Value(4).innertext)
SettingsBase.Item(Item.Key) = sColor
Case "sFont"
Dim FS As FontStyle = Item.Value(3).innertext
Dim sFont As New Font(CStr(Item.Value(1).innertext), CSng(Item.Value(2).innertext), FS)
SettingsBase.Item(Item.Key) = sFont
End Select
End If
Else
SettingsBase.Item(Item.Key) = Item.Value
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
End If
End Sub
Private Sub userOverride_SettingsSaving(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles SettingsBase.SettingsSaving
Dim clssettings As New AppSettings.clsSettings
For Each appProperty In (From xItem In SettingsBase.Properties.OfType(Of System.Configuration.SettingsProperty)() Order By xItem.Name)
Dim clsSetting As New AppSettings.clsSettings.clsSetting
clsSetting.Key = appProperty.Name
If TypeOf SettingsBase.Item(appProperty.Name) Is Font Then
Dim OrigValue As Font = SettingsBase.Item(appProperty.Name)
Dim ValueItem As New AppSettings.sFont
ValueItem.FontName = OrigValue.OriginalFontName
ValueItem.Style = OrigValue.Style
ValueItem.Size = OrigValue.Size
clsSetting.Value = ValueItem
ElseIf TypeOf SettingsBase.Item(appProperty.Name) Is Color Then
Dim OrigValue As Color = SettingsBase.Item(appProperty.Name)
Dim ValueItem As New AppSettings.sColor
ValueItem.A = OrigValue.A
ValueItem.R = OrigValue.R
ValueItem.G = OrigValue.G
ValueItem.B = OrigValue.B
clsSetting.Value = ValueItem
Else
clsSetting.Value = SettingsBase.Item(appProperty.Name)
End If
Dim Desc = From xItem As System.Collections.DictionaryEntry In appProperty.Attributes _
Where TypeOf xItem.Value Is System.Configuration.SettingsDescriptionAttribute _
Select CType(xItem.Value, System.Configuration.SettingsDescriptionAttribute)
If Desc.Count > 0 Then
'save the description
clsSetting.Desc = Desc(0).Description
End If
clssettings.Setting.Add(clsSetting)
Next
Dim serializer As XmlSerializer = New XmlSerializer(GetType(AppSettings.clsSettings), New Type() {GetType(Color), GetType(System.Drawing.Size), GetType(AppSettings.sFont), GetType(AppSettings.sColor)})
Dim tw As TextWriter = New StreamWriter(GetSettingsFile)
serializer.Serialize(tw, clssettings)
tw.Close()
End Sub
End Class
End Namespace