I am using following code to replace the passwords in my app.config. It replaces successfully but does not reload config file in the memory so datasets give error of wrong password.
Please help
Dim vrTextFind As String = "Password"
Dim vrTextReplaceWith As String = "PWD"
Dim path As String = "D:\VS2008\EncTest\EncTest\bin\Debug\enctest.exe.config"
Dim readText As String = File.ReadAllText(path)
TextBox1.Text = readText
'Find
Dim idx As Integer = 0
idx = TextBox1.Text.IndexOf(vrTextFind, idx)
If idx = -1 Then
MessageBox.Show(vrTextFind & " is not in Textbox1")
Else
TextBox1.SelectionStart = idx
TextBox1.SelectionLength = vrTextFind.Length
End If
'Replace
If TextBox1.Text.Contains(TextBox1.Text) Then
TextBox1.Text = TextBox1.Text.Replace(vrTextFind, vrTextReplaceWith)
Else
MessageBox.Show(TextBox1.Text & " is not in Textbox3")
End If
'''''
'Write all back
File.WriteAllText(path, TextBox1.Text)
'Refreshes the connection string section
ConfigurationManager.RefreshSection("connectionStrings")
In windows application
app.configis read only once, when the application starts. If you modify it you will need to restart the app.Suggestion
Instead of storing password in
app.configstore it in some other file (like Settings file). Which can be modified and read at runtime. ForSettingsyou can read on MSDN. And chooseUser-Scopesettings.Hope this helps you.