472,119 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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

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
4 35821
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
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
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
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.

Similar topics

2 posts views Thread by TK | last post: by
8 posts views Thread by vadim | last post: by
2 posts views Thread by Kevin Jackson | last post: by
18 posts views Thread by Praveen Ramesh | last post: by
3 posts views Thread by Codex Twin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.