423,309 Members | 2,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,309 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello,

"jasiglam" <ju*********@hotmail.com> schrieb:
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
--
MVP VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #2

P: n/a
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
Nov 19 '05 #3

P: n/a
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
(th**********@ntlworld.com)

"Go down with your server"

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

Please respond to the newsgroup,
so all can benefit
"jasiglam" <ju*********@hotmail.com> wrote in message
news:00****************************@phx.gbl...
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.

Nov 19 '05 #4

P: n/a
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.
Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.