Problem reading/writing to a config file.

G

Guest

A friend handed me a copy of MS Press's 'Programming ADO.Net 2.0'. There is
a bit of code on pg 99 regarding config files. I have been trying to write a
demo program based on this for my own understanding. The purpose (I thought)
was quite simple: to create an editing screen for config strings, to read,
write, and update them. Unfortunately, while I can add strings, I can't seem
to update them. I keep getting 'configuration is read only' messages.
Here's the code for my Save button.

Dim setting As ConnectionStringSettings
Dim config As Configuration
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

Try

setting = ConfigurationManager.ConnectionStrings(txtName.Text)

If Not setting Is Nothing Then
' An attempt to get around the read-only problem.
ConfigurationManager.ConnectionStrings.Remove(txtName.Text)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
End If

setting = New ConnectionStringSettings()
setting.Name = txtName.Text
setting.ConnectionString = txtContent.Text

config.ConnectionStrings.ConnectionStrings.Add(setting)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)

Catch ex As Exception
MsgBox(ex.Message)
End Try
 
K

Kevin Spencer

That would be because the configuration file is read only. It would be
possible to overwrite it but not via the ConfigurationManager. Also, if you
were to change its' contents, the settings would not take effect until the
application was re-started.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Guest

You'll excuse me if I'm totally baffled.

If I may quote from that same book: "Version 2.0 of the .Net Framework makes
it even easier to modify configuration files programmatically--accessing,
adding, or editing entries in the configuration file for the whole machine,
the entire application, or just the application settings for only the current
user."

That looks like a license to edit the config file. (In any case how can it
be read-only when I HAVE written to it? I just can't update existing
elements.)
 
G

Guest

Apologies for the previous entry. I think I see what you meant. (Here's
some edit code that does what I intended.)

Dim setting As ConnectionStringSettings
Dim config As Configuration
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

Try
setting = ConfigurationManager.ConnectionStrings(txtName.Text)
If Not setting Is Nothing Then

config.ConnectionStrings.ConnectionStrings.Remove(txtName.Text)

config.Save(System.Configuration.ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("connectionStrings")

End If

setting = New ConnectionStringSettings()
setting.Name = txtName.Text
setting.ConnectionString = txtContent.Text

config.ConnectionStrings.ConnectionStrings.Add(setting)

config.Save(System.Configuration.ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("connectionStrings")

Catch ex As Exception
MsgBox(ex.Message)
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top