Programmatically write to app.config file in vb.net code

J

jasiglam

Using the visual studio.net text editor, I know how to
enter/change the "key/value" in the app.config file, for
example:

key="SqlConnection1.ConnectionString" value="data
source=Boomer;initial catalog=TimeDb;integrated
security=SSPI;persist security info=False;workstation
id=BOOMER;packet size=4096"

But I want to be able to change some values, (e.g. data
source string) in vb code programmatically at run time.
Can this be done? If so, how?

Thanks in advance for your help.
 
H

Herfried K. Wagner [MVP]

Hello,

jasiglam said:
Using the visual studio.net text editor, I know how to
enter/change the "key/value" in the app.config file, for
example:

key="SqlConnection1.ConnectionString" value="data
source=Boomer;initial catalog=TimeDb;integrated
security=SSPI;persist security info=False;workstation
id=BOOMER;packet size=4096"

But I want to be able to change some values, (e.g. data
source string) in vb code programmatically at run time.
Can this be done? If so, how?

You can do that using the XML classes contained in the framework.
Nevertheless the config file should not be modified by the application.

HTH,
Herfried K. Wagner
 
F

Fergus Cooney

Hi Jasiglam,

There's a help topic: Introduction to Dynamic Properties,
ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vboriintroductiontoapplicationsetting
storage.htm

Unfortunately everywhere that it talks about changing the config items,
it says to open the file in an editor and type away!!

As it's an XML file, however, you should be able to read it in, change
it and write it back out again using an XMLDocument. I say "should" because
the file may be locked during execution - I don't know.

You should be aware that "app.config" is used when developing but your
program, "appFoo", say, will use "appFoo.exe.config".

If you are talking about a web app, changes to the config file cause the
web app to restart.

Good luck,
Fergus
 
T

Tom Spink

You shouldn't actually be doing this, because app.config files are
application configuration, and not application preferences configuration,
but if you must, the XML classes (In the System.XML namespace) will help
you.

--
Happy to help,
-- Tom Spink
([email protected])

"Go down with your server"

http://dotnetx.betasafe.com >> On The Mend

Please respond to the newsgroup,
so all can benefit
 
J

jasiglam

Thank you for your time and help. In my application, I
need to change the connection string to a database in SQL
Server based on the user's input. The purpose is to
regulate which user can access which table using SQL
Server's security mechanisms.

In VS Studio "Help", under "ADO.NET, connections", in a
document "Introduction to ADO.NET Connection Design
Tools", in the section "Configurable Connection
Properties", they suggested the following (see below),
which clearly indicates that changing the app.config file
by the program is possible.

If I don't use the app.config file to do this, do you have
any suggestions on how to programmatically change the
connection string? Note that this is a Windows app (not
Web-based) and I am using Visual Studio to generate the
adapter and connection objects for every form.


Thanks, again for your help.

(Quote from VS Help)
Configurable Connection Properties

In many applications, connection information cannot be
determined at design time. For example, in an application
that will be distributed to many different customers, you
might not be able to determine at design time the
connection information (such as the name of a server, a
user name, or a password).

Connection strings are therefore often specified as
dynamic properties. Because dynamic properties are stored
in a configuration file (and not compiled into the
application binary files), they can be changed without
having to recompile the application.

A typical strategy is to specify connection properties as
dynamic properties, provide a way (such as a Windows Form
or Web Form page) for users to specify the relevant
details, and then update the configuration file. The
dynamic-property mechanism built into the .NET Framework
automatically gets the values from the configuration file
when the property is read, and updates the file when the
value is updated.
 

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