Dim i As
Integer = 0
Dim config
As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
Dim appSettings
As AppSettingsSection =
CType(config.GetSection("appSettings"), AppSettingsSection)
Dim appKeys
As String() = appSettings.Settings.AllKeys
For i = 0 To appSettings.Settings.Count - 1
Dim FileLocation As String = "C:\Inetpub\wwwroot\YourProject\web.config"
Dim NodeLocation As String = "configuration/appSettings"
'Adds a key and value to the configuration file
Public Function AddKey(ByVal strKey As String, ByVal strValue As String) As Boolean
Dim xmlDoc As New XmlDocument
'Change this to the location of your configuration file
xmlDoc.Load(FileLocation)
'Change this if node is different
Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
Try
'Check if the node exists before adding it
If (KeyExists(strKey)) Then
Throw New ArgumentException("Key name: <" + strKey + "> already exists in the configuration.")
End If
'You must have at least one key that can be empty
Dim newChild As XmlNode = appSettingsNode.FirstChild.Clone
newChild.Attributes("key").Value = strKey
newChild.Attributes("value").Value = strValue
appSettingsNode.AppendChild(newChild)
xmlDoc.Save(FileLocation )
Return True
Catch ex As Exception
Return False
End Try
End Function
'Determines if a key exists
Public Function KeyExists(ByVal strKey As String) As Boolean
Dim xmlDoc As New XmlDocument
xmlDoc.Load(FileLocation )
Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
Dim childNode As XmlNode
For Each childNode In appSettingsNode
If (childNode.Attributes("key").Value = strKey) Then
Return True
End If
Next
Return False
End Function
Here is my web.config file that i'm trying to update dynamically using Hannous code and I receive null as XmlNode. But this works only if i remove xmlns from the web.config. Could you please help me out as it is very urgent for me.
<appSettings>
<!-- point to the database you want to use-->
<add key="ConnStr" value="Server=servername; database=databasename;Integrated Security=SSPI;Connect Timeout=0" />
<!-- specify the full path of the rolespace.xml-->
<add key="ROLESPACE" value="E:\webroot\OAWebService\oaweb.xml" />
<add key="TOCLevelDepth" value="3" />
<add key="SearchMax" value="500" />
</appSettings>
Dim XmlNamespaceManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
XmlNamespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0")
Hi Thanks for your prompt response. But It did not worked still the value is giving null for appsettingsNode.
In the web.config file the namespace prefix is xmlns(which is reserve key word) and in the code we are using 'ns' as the prefix. Hope coz of this it failed.
amersafi
Member
240 Points
114 Posts
(Writing to Web.config file dynamically
Jan 10, 2007 05:52 AM|LINK
i have web.config that have more than value like this
<
appSettings><
add key="lang" value="en"/><
add key="langCode" value="cmsv9_lang"/><
add key="SiteURL" value="/CMS9_Ajax_Website"/><
add key="RealURL" value="/10.10.0.253"/><
add key="SiteUserUrl" value="/User"/><
add key="SiteAdminUrl" value="/Admin"/><
add key="ImageUrl" value="/Images"/><
add key="ScriptUrl" value="/JS"/><
add key="WordsFilePath" value="/App_Data/xml/Localization_word.xml"/><
add key="LanguagesFilePath" value="/App_Data/xml/Localization_Languages.xml"/></
appSettings>i can read this like this
Dim i As Integer = 0 Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim appSettings As AppSettingsSection = CType(config.GetSection("appSettings"), AppSettingsSection) Dim appKeys As String() = appSettings.Settings.AllKeys For i = 0 To appSettings.Settings.Count - 1objHT.Add(appSettings.Settings(appKeys(i)).Key, appSettings.Settings(appKeys(i)).Value)
Nextbut my question how can i update or add value in web.config dynamically from code
Hope4sun
Participant
1139 Points
201 Posts
Re: (Writing to Web.config file dynamically
Jan 10, 2007 10:59 AM|LINK
hannous
Member
203 Points
56 Posts
Re: (Writing to Web.config file dynamically
Jan 10, 2007 11:50 AM|LINK
Here's how you can add a key:
Dim FileLocation As String = "C:\Inetpub\wwwroot\YourProject\web.config" Dim NodeLocation As String = "configuration/appSettings" 'Adds a key and value to the configuration file Public Function AddKey(ByVal strKey As String, ByVal strValue As String) As Boolean Dim xmlDoc As New XmlDocument 'Change this to the location of your configuration file xmlDoc.Load(FileLocation) 'Change this if node is different Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation) Try 'Check if the node exists before adding it If (KeyExists(strKey)) Then Throw New ArgumentException("Key name: <" + strKey + "> already exists in the configuration.") End If 'You must have at least one key that can be empty Dim newChild As XmlNode = appSettingsNode.FirstChild.Clone newChild.Attributes("key").Value = strKey newChild.Attributes("value").Value = strValue appSettingsNode.AppendChild(newChild) xmlDoc.Save(FileLocation ) Return True Catch ex As Exception Return False End Try End Function 'Determines if a key exists Public Function KeyExists(ByVal strKey As String) As Boolean Dim xmlDoc As New XmlDocument xmlDoc.Load(FileLocation ) Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation) Dim childNode As XmlNode For Each childNode In appSettingsNode If (childNode.Attributes("key").Value = strKey) Then Return True End If Next Return False End Functionamersafi
Member
240 Points
114 Posts
Re: (Writing to Web.config file dynamically
Jan 10, 2007 12:43 PM|LINK
hajaworld
Member
24 Points
12 Posts
Re: (Writing to Web.config file dynamically
Sep 18, 2007 10:59 AM|LINK
hi i am tried your concept,but it arise error when execute save line
error is;Access to the path is denied
help me
hajaworld@yahoo.co.in
Haja
mail id:hajaworld@yahoo.co.in
Dotnet Developer
hannous
Member
203 Points
56 Posts
Re: (Writing to Web.config file dynamically
Sep 18, 2007 11:09 AM|LINK
syedrizwanshah
Member
14 Points
10 Posts
Re: (Writing to Web.config file dynamically
Oct 18, 2007 05:34 AM|LINK
This may help
Use configSource attribute to manage Web.Config sections in ASP.Net 2.0
http://rizwanshah.blogspot.com/2007/10/use-configsource-attribute-to-manage.html
kanthu2681
Member
24 Points
14 Posts
Re: (Writing to Web.config file dynamically
Jun 25, 2008 09:42 AM|LINK
Here is my web.config file that i'm trying to update dynamically using Hannous code and I receive null as XmlNode. But this works only if i remove xmlns from the web.config. Could you please help me out as it is very urgent for me.
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<appSettings>
<!-- point to the database you want to use-->
<add key="ConnStr" value="Server=servername; database=databasename;Integrated Security=SSPI;Connect Timeout=0" />
<!-- specify the full path of the rolespace.xml-->
<add key="ROLESPACE" value="E:\webroot\OAWebService\oaweb.xml" />
<add key="TOCLevelDepth" value="3" />
<add key="SearchMax" value="500" />
</appSettings>
</configuration>
Thanks!!!
hannous
Member
203 Points
56 Posts
Re: (Writing to Web.config file dynamically
Jun 25, 2008 03:45 PM|LINK
To care for the namespace, add the following:
After:
Add:
kanthu2681
Member
24 Points
14 Posts
Re: (Writing to Web.config file dynamically
Jun 26, 2008 09:34 AM|LINK
Hi Thanks for your prompt response. But It did not worked still the value is giving null for appsettingsNode.
In the web.config file the namespace prefix is xmlns(which is reserve key word) and in the code we are using 'ns' as the prefix. Hope coz of this it failed.
Kindly, let me know where i'm going wrong.