Update Application Setting in Windows Forms 2.0

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My understanding is that when using My.Settings, you can update the user
level settings but the application level settings are read-only. Is there
some means by which you can update an application level setting? Any insight
would be greatly appreciated.

Thanks,

Dennis
 
Hi Dennis,

Yes, in .NET 2.0, application-scoped settings are readonly at run time,
because these values are usually associated with the application. This
behavior is by design.

If you'd like to modify an application-scoped setting at run time, you
could do this like below:
' Setting1 is an application-scoped setting
My.Settings("Setting1") = "new setting value"
' MyConnectionString is a connection string setting
My.Settings("MyConnectionString") = "new connection string"

If you like to save the changes back to the configuration file, you could
use System.Configuration.ConfigurationManager and Configuration classes to
do it. The following is sample code. Note that you need to add a reference
to the System.Configuration in the project.

Imports System.Configuration

Private Sub WriteSetting(ByVal sectionGroupName As String, ByVal
sectionName As String, ByVal settingName As String, ByVal newvalue As
String)
Dim configuration As Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
' Get sectionGroup
Dim sectionGroup As ConfigurationSectionGroup =
configuration.GetSectionGroup(sectionGroupName)
' Get section
Dim section As ClientSettingsSection =
CType(sectionGroup.Sections.Get(sectionName), ClientSettingsSection)
' Get setting
Dim setting As SettingElement = section.Settings.Get(settingName)
' set the new value to the setting
setting.Value.ValueXml.InnerText = newvalue
' ensure the changes will be saved back to the configuration file
section.SectionInformation.ForceSave = True
' save the changes to the configuration file
configuration.Save()
End Sub

Then you could call this 'WriteSetting' method to save the
application-scoped setting. Note that this method can also be used to save
user-scroped setting. The following is a sample to call the 'WriteSetting'
method:

WriteSetting("applicationSettings", "WindowsApplication1.My.MySettings",
"Setting1", "new setting value")

BTW, connection string is a special application-scoped setting, which has a
different programming model from normal application-scoped settings. We
could still use ConfigurationManager and Configuration classes to save
connection string setting to the configuration file, but with a little
difference. The following is a sample.

Private Sub WriteConnectionString(ByVal settingName As String, ByVal
newvalue As String)
Dim configuration As Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim connSection As ConnectionStringsSection =
configuration.ConnectionStrings
Dim connSetting As ConnectionStringSettings =
connSection.ConnectionStrings(settingName)
connSetting.ConnectionString = newvalue
connSection.SectionInformation.ForceSave = True
configuration.Save()
End Sub

The following is a sample to call the 'WriteConnectionString' method:

WriteConnectionString("WindowsApplication1.My.MySettings.MyConnectionString"
, "new connection string")

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Dennis - Here's an article that I found useful for understanding how to
modify the application level settings.
 
Back
Top