Specialized.NameValueCollection

K

Kerr

Hi all,
I've been scouring the internet for help with this problem and every
occurance i've seen reconstructs the problem but no one seems to have a
solution. Hoping that you guys can help me.

I have a vb.net windows forms project that is using a app.config file.
the contents of the app.config is staggered. see a snippet below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- tell .NET Framework to ignore CLR sections -->
<section name="frisbee"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmBatching"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmFiltering"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmConfig"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmSupporting"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<frisbee>
<add key="intErrorLogtype" value="1" />
<add key="EventLogKeyName" value="FRISBEE" />
<add key="SQLDBConnString" value="" />
<add key="sys_DBCheck" value="usp_CheckSQLConnection" />
<add key="Maximised" value="true" />
</frisbee>
<frmBatching>
<add key="height" value="" />
<add key="width" value="" />
<add key="top-left-position" value="" />
<add key="ImgDir" value="" />
<add key="BatchDir" value="" />
<add key="ImgInBatch" value="" />
<add key="BatchNo" value="" />
<add key="Maximised" value="true" />
</frmBatching>


Now to read values out of this staggered app.config file i use the
specialised.namevaluecollection in the following manner

Dim objConfigNV As New Specialized.NameValueCollection
Dim strConfigValue As String = Convert.ToString(vbNullString)

objConfigNV = CType(ConfigurationSettings.GetConfig(strIDicName),
Specialized.NameValueCollection)

strConfigValue = Convert.ToString(objConfigNV(strKeyName))

And this works fine.

Now.. the problem..

to write a value back to the app.config file using the same
Specialized.NamevalueCollection in the following manner:

'initialise collection object of app.config
Dim objNameValue As New Specialized.NameValueCollection

'app.ocnfig is staggered. this gets the relevant sectoin on
file
objNameValue =
CType(ConfigurationSettings.GetConfig("frmSupporting"),
Specialized.NameValueCollection)

'this sets the the new value of a key
objNameValue(strKey) = strValue


Generates an exception: Collection is read-only


Does ANYONE have any ideas how to create an instance of a
NameValueCollection that is not read-only so that I can write back to my
app.config file.


Any help on this would be greatly appreciated.

Kerr
 
C

Chris Dunaway

App.config file was never meant to be written to using the
NameValueCollection. It was not meant to store user settings. The
only way to write back to it would be to use the methods in the
System.Xml namespace.

What I typically do is design a class that models all the settings I
need and then I make that class serializable. Then I just need to
serialize and deserialize to/from the xml file and I have an object
with all my settings.
 
K

Kerr

Chris,
Thanks for your reply. Unfortunately you've over estimated my .net
knowledge.

What you've described makes sense but I need to see a practical example
of what you describe for me to be able to implement a solution that
works.

I hear what your saying about not using the namevaluecollection to write
to the app.config file but because I am extending the use of the
app.config file by adding in the additional sections I was pointed at
using this method by other forums.

Because I am not sure how to implement your suggestion my question still
stands, which is, do you know of a way to create a NON-READ-ONLY version
of the NameValueCollection.

Cheers

Kerr
 
C

Chris Dunaway

There is no way to create a NON-READ-ONLY NameValueCollection that I
know of. But the method below is just a simple.

What I mean is that I create a class with properties that hold the
values that I want to store, then use the XmlSerializer to save that
class with it's values to a file.

Here is a simple example.

Create a new class file and add the code at the end of this post. Then
add a Sub Main or sub with the following code:

Public Sub Main()

'Create an instance of the MySettings class
Dim settings As New MySettings

'Set some of the properties
settings.Frisbee.Maximised = False
settings.frmBatching.Width = 44

'Save the configuration to an .xml file
MySettings.Save("c:\MySettings.xml", settings)


'To load the data from the file use this code:
Dim settings2 As MySettings =
MySettings.Load("c:\MySettings.xml")

'Print the value of the Width property:
Console.WriteLine(settings2.frmBatching.Width.ToString)

'Change the value of the width property
settings2.frmBatching.Width = 100

'Resave the file
MySettings.Save("c:\MySettings.xml", settings2)

End Sub


'Note that the classes are marked as Serializable() and that the
serializer
'will only save the properties and fields that are public. I used
public fields
'in this case, but you could use full properties.

'The Load and Save method are Shared methods for convenience.


'******** MySettings.Vb *******************

Imports System.Xml.Serialization
Imports System.IO

<Serializable()> _
Public Class MySettings

Public Frisbee As FrisbeeClass
Public frmBatching As frmBatchingClass

Public Sub New()
Frisbee = New FrisbeeClass
frmBatching = New frmBatchingClass
End Sub

Shared Function Load(ByVal fname As String) As MySettings
Dim sr As StreamReader

Try
sr = New StreamReader(fname)
Dim xs As New XmlSerializer(GetType(MySettings))
Return DirectCast(xs.Deserialize(sr), MySettings)
Finally
sr.Close()
End Try

End Function

Shared Sub Save(ByVal fname As String, ByVal obj As MySettings)
Dim sw As StreamWriter

Try
sw = New StreamWriter(fname)
Dim xs As New XmlSerializer(GetType(MySettings))

xs.Serialize(sw, obj)
Finally
sw.Close()
End Try

End Sub
End Class

<Serializable()> _
Public Class FrisbeeClass
Public intErrorLogType As Integer
Public EventLogKeyName As String = String.Empty
Public SQLDBConnString As String = String.Empty
Public sys_DBCheck As String = String.Empty
Public Maximised As Boolean
End Class

<Serializable()> _
Public Class frmBatchingClass
Public Height As Integer
Public Width As Integer
Public TopLeft As Integer
Public ImgDir As String = String.Empty
Public BatchDir As String = String.Empty
Public ImgInBatch As String = String.Empty
Public BatchNo As Integer
Public Maximised As Boolean
End Class

'************ END MySettings.vb *******************
 
K

Kerr

Chris,
thanks for the post and the code. Your implementation works and I
understand how it works which is even better. However, I am not sure if
its something i've done wrong but whenever writing back (save) to the
xml document the structure of the document changes and then my entire
app stops working.

I am getting to the stage where i am thinking it is easier to do this
sort of dynamic configuration stuff through a database rather than the
app.config file. I can't spend too much longer trying to get this to
work.

Chris, thanks for your help on this anyway.

Kerr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top