From: Saurabh on
Thanks a lot for this..... Peter Forman

I've been lookin for this code from long time.... thanks for your valuable help....!!



Peter Forman wrote:

Re: Modify app.config in VB.NET
07-Mar-08

On Mar 5, 3:21 pm, "Bill Yanaire" <B...(a)yaniaire.com> wrote:


something along these lines....

Public Shared Sub ChangeConnectionString(ByVal strConn As String)
Dim _config As System.Configuration.Configuration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
'the full name of the connection string can be found in the
app.config file
' in the "name" attribute of the connection string

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn
'Save to file

_config.Save(System.Configuration.ConfigurationSaveMode.Modified)
'force changes to take effect so that we can start using
'this new connection string immediately

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub


....worked for me.

Previous Posts In This Thread:

On Tuesday, March 04, 2008 6:15 PM
Bill Yanaire wrote:

Modify app.config in VB.NET
I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config?

The string is in the following:

<connectionStrings>

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLMachine to MySQLMachine_1



Thank you.

On Tuesday, March 04, 2008 11:53 PM
Cor Ligthert[MVP] wrote:

Bill,Do you mean this?
Bill,

Do you mean this?

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

Cor

On Wednesday, March 05, 2008 2:39 AM
Mr. Arnold wrote:

Re: Modify app.config in VB.NET
What do you mean? Do you want to change it during program execution with a
new value being picked-up?

On Wednesday, March 05, 2008 5:14 AM
kimiraikkonen wrote:

Re: Modify app.config in VB.NET
App.config XML file can be opened and edited by numerous apps such as
Notepad and VS versions if you want to change the values manually.

On Wednesday, March 05, 2008 5:14 AM
JDS wrote:

Re: Modify app.config in VB.NET
On Mar 5, 7:45=A0am, kimiraikkonen <kimiraikkone...(a)gmail.com> wrote:

nitial
=3Dsa1234"
1

I have had a similar problem trying to change the value from code in
VS2005; I get an error saying that it is read only (or something
similar; it is a while since I looked at it). If there is a solution
I'd greatly appreciate it if anyone knows how.

On Wednesday, March 05, 2008 10:17 AM
Bill Yanaire wrote:

Re: Modify app.config in VB.NET
Yes, I would like to change the DataSource when the program starts
execution. Depending on what machine the program is run, I would like to
check the name of the computer then change accordingly.

On Wednesday, March 05, 2008 10:21 AM
Bill Yanaire wrote:

Re: Modify app.config in VB.NET
Thanks for the link, but that does not allow me to modify a connection
string. The code looks like it reads only.

On Wednesday, March 05, 2008 11:58 AM
Mr. Arnold wrote:

Re: Modify app.config in VB.NET
"Bill Yanaire" <Bill(a)yaniaire.com> wrote in message
news:egTxMQtfIHA.5164(a)TK2MSFTNGP03.phx.gbl...

Yes, you can do that. You can get the machine name.

Public Function GetMyDnsName() As String
Return System.Net.Dns.GetHostName()
End Function

Now, if if want to manipulate app.config, then you'll be working with
pgmname.app.config. That's what the program uses and not app.config doing
program execution.

You can download the Application Blocks for 2.0 .Net Framework, which will
have a section of source code and a project that you can look at that works
on the app.config, with reading, writing and changing app.config data during
program execution. There should be a download for 3.0, 3.5 or you can apply
the source code from 2.0, look for it using Google.

On Wednesday, March 05, 2008 3:58 PM
Bill Yanaire wrote:

Re: Modify app.config in VB.NET
"Peter Forman" <xla760(a)gmail.com> wrote in message
news:3ba7c072-0f90-4fc1-90a5-6f0b3e29ba1e(a)n36g2000hse.googlegroups.com...

I just tried to copy your code into a test application. Received an error
'System.Configuration is not defined'. I have an 'Imports
System.Configuration' at the top of the form.

Any idea why?

Thanks

On Wednesday, March 05, 2008 4:05 PM
Herfried K. Wagner [MVP] wrote:

Re: Modify app.config in VB.NET
"Bill Yanaire" <Bill(a)yaniaire.com> schrieb:

Make sure your project contains a reference to "System.Configuration.dll".

Note that the code above will only work if the user has write privileges for
the config file, which is by default not the case for normal users.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

On Wednesday, March 05, 2008 4:20 PM
Bill Yanaire wrote:

Re: Modify app.config in VB.NET
"Herfried K. Wagner [MVP]" <hirf-spam-me-here(a)gmx.at> wrote in message
news:u5U%23vSwfIHA.1132(a)TK2MSFTNGP06.phx.gbl...

Thanks for the tip on the System.Configuration.dll. That corrected the
errors. Regarding the following:

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn


doesn't this change the MySettings or will it also change the
<connectionStrings>? I would like to change the

<connectionStrings>

<add name="ConnString" connectionString="Data Source=MySQLServer;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLServer to YourSQLServer.




Thanks

On Friday, March 07, 2008 11:24 PM
Peter Forman wrote:

Re: Modify app.config in VB.NET
On Mar 5, 3:21 pm, "Bill Yanaire" <B...(a)yaniaire.com> wrote:


something along these lines....

Public Shared Sub ChangeConnectionString(ByVal strConn As String)
Dim _config As System.Configuration.Configuration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
'the full name of the connection string can be found in the
app.config file
' in the "name" attribute of the connection string

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn
'Save to file

_config.Save(System.Configuration.ConfigurationSaveMode.Modified)
'force changes to take effect so that we can start using
'this new connection string immediately

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub


....worked for me.

On Thursday, March 13, 2008 12:10 PM
Andre wrote:

Hi,I've tried the code out with reference from msdn and I've got the same
Hi,

I've tried the code out with reference from msdn and I've got the same
problem as well.

When going thru the stepbystep debugger, I can see the values being added to
the ConfigurationManager, but somehow the values are not being save to the
app.config file.

eg.

using System.Configuration;

ConfigurationSection section =
(ConfigurationSection)config.GetSection("appSettings");

//Read the key in the app.config file
AppSettingsSection assection = config.AppSettings;
string str = assection.Settings["aaa"].Value.ToString();

// Add an Application Setting.
config.AppSettings.Settings.Add(asName, "test_value");

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Is there an error in my code ? Any feedback is appreciated.

cheers,
Andrew

On Friday, April 25, 2008 4:41 PM
Levent Dagistanl wrote:

I think you are running your code from VS.
I think you are running your code from VS. Just run the exe from outside and
it will work.
Thanks,
Levent Dagistanli

"Andrew" wrote:

On Saturday, April 26, 2008 4:39 AM
Mike Williams wrote:

Re: Modify app.config in VB.NET
"Levent Dagistanli" <Levent Dagistanli(a)discussions.microsoft.com> wrote in
message news:5829BD27-332C-4C57-8BF8-B67C934949E2(a)microsoft.com...


I don't know whether anyone from Microsoft regularly reads the Visual Basic
groups but if they do then I am very surprised that they permit one of their
own MVPs to engage in such outrageous long term trolling activities in one
of their own public newsgroups, such as the activity that the person who
purports to be Bill McCarthy has engaged in on the
microsoft.public.vb.general.discussion group for many months. If this man
belongs to you:

https://mvp.support.microsoft.com/profile=B2D0BB02-3E35-4293-B4B9-25680609CCB8

.. . . then perhaps you might like to look at his activity in that group.
Here for example is one of his very latest offerings:

"Bill McCarthy" <Bill(a)N0SPAM.com> wrote in message
news:19A5DEEA-ED6A-4721-9DB9-9F5D8509D825(a)microsoft.com...


Submitted via EggHeadCafe - Software Developer Portal of Choice
EggHeadCafe Chat Chaos in Silverlight Released Today
http://www.eggheadcafe.com/tutorials/aspnet/325ea67e-d6c4-4811-b096-54f31bdede5d/eggheadcafe-chat-chaos-in.aspx