added samples
[windows-sources.git] / sdk / samples / FrameworkSamples / NETCF / Technologies / XLINQ / vb / xlinqsamplebooks / configurationmanager.vb
blobd2a44726135f67236a110a4affc9d7cfc8a823e3
1 ''---------------------------------------------------------------------
2 '' This file is part of the Microsoft .NET Framework SDK Code Samples.
3 ''
4 '' Copyright (C) Microsoft Corporation. All rights reserved.
5 ''
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.
9 ''
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
13 ''PARTICULAR PURPOSE.
14 ''---------------------------------------------------------------------
15 Imports System.Collections.Specialized
16 Imports System.Reflection
17 Imports System.Linq
18 Imports System.Xml.Linq
19 Imports System.IO
20 Imports System.Globalization
23 ''' <summary>
24 ''' Class to Read /Write Configuration Settings from the App Config file
25 ''' </summary>
26 Class ConfigurationManager
28 ''' <summary>
29 ''' private NameValueCollection that stores the key value config settings
30 ''' </summary>
31 Private Shared appSettingsValue As NameValueCollection
33 ''' <summary>
34 ''' Public property that returns the app config settings
35 ''' </summary>
36 Public Shared Property AppSettings() As NameValueCollection
37 Get
38 'if not loaded, then load app config settings
39 If appSettingsValue Is Nothing Then
40 LoadApplicationSettings()
41 End If
43 Return appSettingsValue
44 End Get
45 Set(ByVal value As NameValueCollection)
46 appSettingsValue = value
47 End Set
48 End Property
50 ''' <summary>
51 ''' Save the new set of config settings back to the app config file.
52 ''' </summary>
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)
61 'check for existence
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")))
67 Else
68 doc = XDocument.Load(name)
69 End If
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.
87 doc.Save(name)
89 End Sub
93 ''' <summary>
94 ''' Loads the key value pairs on appSettings Element to a NameValueCollection
95 ''' </summary>
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
123 For Each v In q
124 AppSettings.Add(v.key, v.value)
125 Next
127 End If
130 End Sub
133 End Class