the following is the code for the custom SettingsProvider from Geoff and mine's TechEd 2006 presentation:
Option Strict On
Option Explicit On
Imports System
Imports System.Configuration
''' <summary>
''' simple example of a SetitngsProvider that saves each individual setting in it's own file
''' </summary>
''' <remarks>to use, add a setitng to My.Settigns designer, open the properties window
''' and set the provider to FileByFileSettingsProvider if in the same project, otherwise the full name.
''' </remarks>
Public Class FileByFileSettingsProvider
Inherits SettingsProvider
#Region "constructor and initialize"
Public Sub New()
MyBase.new()
End Sub
Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
If name Is Nothing Then name = My.Application.Info.ProductName
m_appName = name
MyBase.Initialize(m_appName, config)
End Sub
Private m_appName As String
Public Overrides Property ApplicationName() As String
Get
Return m_appName
End Get
Set(ByVal value As String)
m_appName = value
End Set
End Property
#End Region
'HACK: hard coded root path. In practice use assembly path or similar
Private m_rootPath As String = "E:\test\"
Public Overrides Function GetPropertyValues(ByVal context As SettingsContext, _
ByVal properties As SettingsPropertyCollection) _
As SettingsPropertyValueCollection
Dim values As New SettingsPropertyValueCollection
For Each setting As SettingsProperty In properties
Dim filepath As String = m_rootPath & setting.Name & ".xml"
Dim value As New SettingsPropertyValue(setting)
With My.Computer.FileSystem
If .FileExists(filepath) Then
value.SerializedValue = .ReadAllText(filepath)
' coment out the preceeding line and uncomment the following line to make the serialization binary
'value.PropertyValue = GetObjectFromBytes(.ReadAllBytes(filepath))
End If
value.IsDirty = False
values.Add(value)
End With
Next
Return values
End Function
Public Overrides Sub SetPropertyValues(ByVal context As SettingsContext, _
ByVal values As SettingsPropertyValueCollection)
For Each value As SettingsPropertyValue In values
Dim filepath As String = m_rootPath & value.Name & ".xml"
My.Computer.FileSystem.WriteAllText(filepath, value.SerializedValue.ToString, False)
' coment out the preceeding line and uncomment the following line to make the serialization binary
' My.Computer.FileSystem.WriteAllBytes(filepath, GetObjectBytes(value.PropertyValue), False)
Next
End Sub
#Region "helper methods"
'HACK: this is sub-optimal means of reading the binary stream from disk.
Private Function GetObjectBytes(ByVal obj As Object) As Byte()
Using buffer As New IO.MemoryStream()
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
formatter.Serialize(buffer, obj)
Return buffer.GetBuffer
End Using
End Function
'HACK: this is sub-optimal means of writing out the binary stream to disk.
Private Function GetObjectFromBytes(ByVal bytes As Byte()) As Object
Using buffer As New IO.MemoryStream(bytes)
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Return formatter.Deserialize(buffer)
End Using
End Function
#End Region
End Class