Disclaimer
I am not in any way involved in the administration of The Code Project, and the administrators have not, in any capacity, asked me to write this article. This article is completely based on my own personal views and experiences in programming and technical writing.
Introduction
I searched on Google and did not find any exact results of resolving my problem “How can we change the applicationsettings values from a different project?” Finally I found something but there weren't many details available, so by adding some code I got how it can be done. This article shows how you can change the applicationsettings and usersettings section of config file at runtime. You can use it to create a project like Configuration Manager to manage multiple config files. You can also find an exciting resource on editing the appsetting key/value section at Edit and Encrypt Web.Config Sections Using C# 2.0.
Background
As per the MSDN - Application settings allow you to store and retrieve property settings and other information for your application dynamically, and they allow you to maintain custom application and user preferences on the client computer. You might want to store a user's color preferences, and then retrieve them the next time the application runs. Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string. In Visual Basic projects, you can access application settings at run time using the My.Settings object. But I want to modify these settings by another project, so I have to write some custom code for accomplishing my task.
How the Application Runs
- When you run the source project, you get the following screen:
- Click the Load button, which loads the below settings in
DataGridView:
- After changing the setting values:
- Click the Update button to update the changed values to config file.
Using the Code
- Create a Web config file and add the following settings using the Solution->Property->Settings.
See the app.config file now. The application settings are added.
- Create a class named
ConfigSettings as below:
Public Class ConfigSettings
Private _settingName As String
Private _settingValue As String
Private _serializeAs As String
Public Property SettingName() As String
Get
Return _settingName
End Get
Set(ByVal value As String)
_settingName = value
End Set
End Property
Public Property SettingValue() As String
Get
Return _settingValue
End Get
Set(ByVal value As String)
_settingValue = value
End Set
End Property
Public Property SerializeAs() As String
Get
Return _serializeAs
End Get
Set(ByVal value As String)
_serializeAs = value
End Set
End Property
End Class
- Open the form and add the following controls:
DataGridView and name it dgvAppSetting
- Load File button and name it
btnLoadFile
Update button and name it btnUpdate
See the following image:
- Add the following global variables:
Private m_strSettingName As String
Private ConfigSettingsList As New List(Of ConfigSettings)
- Write the code shown below in the click event of
btnLoadFile button:
Private Sub btnLoadFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoadFile.Click
Try
Dim strFile As String
Dim ConfigDoc As New XmlDocument()
strFile = String.Format("{0}/_
ReadConfigurationAppSettingSection.exe.config", _
Directory.GetCurrentDirectory.ToString())
ConfigDoc.Load(strFile)
Dim applicationSettingsNode As XmlNode = _
ConfigDoc.GetElementsByTagName("applicationSettings")(0)
ConfigDoc.GetElementsByTagName("userSettings")(0)
ConfigSettingsList.Clear()
For Each objXmlNode As XmlNode _
In applicationSettingsNode.FirstChild.ChildNodes
Dim objConfigSettings As New ConfigSettings
With objConfigSettings
.SettingName = objXmlNode.Attributes(0).Value
.SerializeAs = objXmlNode.Attributes(1).Value
.SettingValue = objXmlNode.FirstChild.InnerText
End With
ConfigSettingsList.Add(objConfigSettings)
Next
ConfigSettingsBindingSource.DataSource = ConfigSettingsList
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End Try
End Sub
End Class
- Write a method which is used as predicate for finding the setting in collection:
Private Function GetSetting(ByVal objConfigSettings As ConfigSettings) _
As Boolean
Return m_strSettingName = objConfigSettings.SettingName
End Function
- Write the below code in the click event of
btnUpdate button:
Private Sub btnUpdate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnUpdate.Click
Try
Dim strFile As String
Dim ConfigDoc As New XmlDocument()
strFile = String.Format("{0}/_
ReadConfigurationAppSettingSection.exe.config", _
Directory.GetCurrentDirectory.ToString())
ConfigDoc.Load(strFile)
Dim applicationSettingsNode As XmlNode = _
ConfigDoc.GetElementsByTagName("applicationSettings")(0)
ConfigDoc.GetElementsByTagName("userSettings")(0)
For Each objXmlNode As XmlNode _
In applicationSettingsNode.FirstChild.ChildNodes
m_strSettingName = objXmlNode.Attributes(0).Value
Dim objConfigSettings As ConfigSettings = _
ConfigSettingsList.Find(AddressOf GetSetting)
If objConfigSettings IsNot Nothing Then
With objConfigSettings
objXmlNode.Attributes(0).Value = .SettingName
objXmlNode.Attributes(1).Value = .SerializeAs
objXmlNode.FirstChild.InnerText = .SettingValue
End With
End If
Next
ConfigDoc.Save(strFile)
MessageBox.Show("Updated Successfully.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
History
- 25th March, 2009: Initial post