1
''---------------------------------------------------------------------
2 '' This file is part of the Microsoft .NET Framework SDK Code Samples.
4 '' Copyright (C) Microsoft Corporation. All rights reserved.
6 ''This source code is intended only as a supplement to Microsoft
7 ''Development Tools and/or on-line documentation. See these other
8 ''materials for detailed information regarding Microsoft code samples.
10 ''THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 ''KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 ''IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 ''---------------------------------------------------------------------
15 Imports System
.Collections
.Specialized
16 Imports System
.Reflection
18 Imports System
.Xml
.Linq
20 Imports System
.Globalization
24 ''' Class to Read /Write Configuration Settings from the App Config file
26 Class ConfigurationManager
29 ''' private NameValueCollection that stores the key value config settings
31 Private Shared appSettingsValue
As NameValueCollection
34 ''' Public property that returns the app config settings
36 Public Shared
Property AppSettings() As NameValueCollection
38 'if not loaded, then load app config settings
39 If appSettingsValue Is
Nothing Then
40 LoadApplicationSettings()
43 Return appSettingsValue
45 Set(ByVal value
As NameValueCollection
)
46 appSettingsValue
= value
51 ''' Save the new set of config settings back to the app config file.
53 Public Shared
Sub Save()
55 'if no settings to save, just return
56 If (AppSettings Is
Nothing) Then Return
58 'get the config file name
59 Dim name
As String = String.Format(CultureInfo
.CurrentCulture
, "{0}.config", Assembly
.GetExecutingAssembly().GetName().CodeBase
)
62 'if config file does not exist, then create new one , else load existing one
63 Dim doc
As XDocument
= Nothing
65 If (Not File
.Exists(name
)) Then
66 doc
= New XDocument(New XElement("configuration", New XElement("appSettings")))
68 doc
= XDocument
.Load(name
)
71 'retrieve the appSettings element in the XML app config file
72 Dim appSetttingsElement
As XElement
= doc
.Element("configuration").Element("appSettings")
74 'remove all the nodes in the appSettings Elements
75 appSetttingsElement
.RemoveNodes()
77 'create a new XElement with the key value pairs as child nodes.
78 Dim newAppSettingsElement
As XElement
= New XElement("appSettings", _
79 From key
In AppSettings
.Keys
.OfType(Of
String)() _
80 Select New XElement("add", New XAttribute("key", key
), _
81 New XAttribute("value", AppSettings(key
))))
83 'replace the existing appSettings element with the newly created one
84 appSetttingsElement
.ReplaceWith(newAppSettingsElement
)
86 'finally save the xml back to the app config file.
94 ''' Loads the key value pairs on appSettings Element to a NameValueCollection
96 Private Shared
Sub LoadApplicationSettings()
97 AppSettings
= New NameValueCollection()
99 'config file name is {applicationName}.exe.config
100 'Assembly.GetExecutingAssembly().GetName().CodeBase returns full path of the current executable
101 Dim name
As String = String.Format(CultureInfo
.CurrentCulture
, "{0}.config", Assembly
.GetExecutingAssembly().GetName().CodeBase
)
103 'check if the config file exists.
104 If (Not File
.Exists(name
)) Then Return
107 'load the configuration file
108 Dim doc
As XDocument
= XDocument
.Load(name
)
111 'check existence configuration element and existence of the appSettings element inside configuration element
112 If (Not doc
.Element("configuration") Is
Nothing And _
113 Not doc
.Element("configuration").Element("appSettings") Is
Nothing) Then
115 'select the elements inside appSettings section named add.
116 'the add element will have two attributes 1)key 2)value
117 Dim q
= From e
In doc
.Element("configuration").Element("appSettings").Elements() _
118 Where
Not e
.Attribute("key") Is
Nothing And Not e
.Attribute("value") Is
Nothing _
119 Select New With {.key
= e
.Attribute("key").Value
, .value
= e
.Attribute("value").Value
}
122 'loop through every element and add the key ,value pair to the NamedValueCollection
124 AppSettings
.Add(v
.key
, v
.value
)