Hierarchical structure in config file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

appSettings in Framework 1.x had the limitation of being "flat". I was just
wondering if the section structure of config files in Framework 2.0 has
overcome this.

Is it now possible to define your own hierarchical structure in the
app.config file so that it will be readable in the My.Settings object?

I want to use it this way:
dirExport = My.Settings.Directories.Export
dirImport = My.Settings.Directories.Import

I tried to play around with sections and section groups in the config file,
but didn't find any working solution.
 
Hi,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi jaklithn,

Thanks for your post.

Yes, in VS.net2005, it really support customized class type. We can define
a customized type, then provide a customized TypeConverter to convert this
class from/to string, then in the application setting designer, we can
explicitly set a property for My.Settings to our customized class type.
Below is a sample:

//my customized class and type converter
Public Class MyTestTypeConverter
Inherits TypeConverter

Public Overrides Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function

Public Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object, ByVal destinationType As Type) As Object

If ((Not value Is Nothing) AndAlso (destinationType Is
GetType(String))) Then
Dim mt As MyTest = CType(value, MyTest)
Return mt.Export & "," & mt.Import

End If

Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function

Public Overrides Function CanConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function

Public Overrides Function ConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object) As Object
Dim text1 As String = TryCast(value, String)
If (Not text1 Is Nothing) Then
Dim index As Integer = text1.IndexOf(",")
Dim mt As New MyTest
mt.Export = text1.Substring(0, index)
mt.Import = text1.Substring(index + 1)
Return mt
End If

Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class

<TypeConverter(GetType(MyTestTypeConverter))> _
Public Class MyTest
Private exp As String
Public Property Export() As String
Get
Return exp
End Get
Set(ByVal value As String)
exp = value
End Set
End Property

Private imp As String
Public Property Import() As String
Get
Return imp
End Get
Set(ByVal value As String)
imp = value
End Set
End Property

End Class

In the application settings designer, I still did not find a way to use the
designer to select our class type. I have tried to deploy my class and type
converter as a separate assembly, strong name it and place it into the GAC,
the designer still igore the customized class. Currently, I just right
click Settings.settings file, use a text editor to open this file and
explicit change our "Directories" property type to "MyTest". Also, I have
changed the type in both Settings.Designer.vb and app.Config files.

Now, after the modification, I can input "ddd,ffff" in the property value
field. And at runtime, we can access/modify this property like this now:
dirExport = My.Settings.Directories.Export
dirImport = My.Settings.Directories.Import

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your answer.
But the whole idea was to have an easy way of managing settings, so I guess
I stick to "long concatenated setting names" .....
 
Hi jaklithn,

Thanks for your feedback.

Yes, because My.Settings must use a defined class as its type, if we want
to use customized strucuture, we have to define a customized type for it
first. Anyway, if you need further help, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top