Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing App Settings

5 views
Skip to first unread message

Andrew

unread,
May 10, 2002, 8:38:14 AM5/10/02
to
I know this is probably a simple question, but could someone please advise
me on the best way in which to stores application settings (db server to
connect to, username, password etc) within a c# windows app??

In vb you could use SaveSetting and Getsetting which put stuff in the
registery..

Cheers

Andy


Stacy

unread,
May 10, 2002, 10:26:38 AM5/10/02
to
Here are two ways to accomplish this:

(first, look up System.Configuration.AppSettingsReader in MSDN.) Then,

1. If you want to save properties of controls at design time and have them
recalled later, most components have a 'DynamicProperties' property that you
can access. Just check the property you want saved and the program does the
rest for you.

2. If you want to do it at run time, here is some sample code (and this is
also what the design time stuff translates to):

System.Configuration.AppSettingsReader configurationAppSettings = new
System.Configuration.AppSettingsReader();
this.oleDbConnection1.ConnectionString =
((string)(configurationAppSettings.GetValue("oleDbConnection1.ConnectionStri
ng", typeof(string))));

This example reads the value of a key called
"oleDbConnection1.ConnectionString" in your application settings file.

Both these methods write to an app.config XML file in your project directory
which you can distribute with your app.

HTH,

Stacy

"Andrew" <an...@dunn-line.com> wrote in message
news:Ol2z2$B#BHA.2040@tkmsftngp04...

Shawn Burke [MSFT]

unread,
May 13, 2002, 2:06:12 PM5/13/02
to
Another way to accomplish this is with something called IsolatedStorage.
Basically the steps are this:

1) Create a Hashtable to store your settings:

Hashtable settings = new Hashtable();
settings["UserName"] = "SomeValue";
settings["Preference1"] = SomeOtherValue;

2) Use IsolatedStorage and formatters to save the values:

private void SaveOptions(Hashtable values)
{
IsolatedStorageFile isoFile =
System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope.
Assembly | IsolatedStorageScope.User, null, null);
IsolatedStorageFileStream stream = new
IsolatedStorageFileStream("mysettings.dat", System.IO.FileMode.Truncate);
System.Runtime.Serialization.IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

formatter.Serialize(stream, values);
stream.Close();
isoFile.Close();
}

3) Use the IsoStore to read those options back in:

private Hashtable LoadOptions()
{
IsolatedStorageFile isoFile =
System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope.
Assembly | IsolatedStorageScope.User, null, null);
IsolatedStorageFileStream stream = new
IsolatedStorageFileStream("mysettings.dat",
System.IO.FileMode.OpenOrCreate);
Hashtable setttings = null;
System.Runtime.Serialization.IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
if (stream != null && stream.Length > 0)
{
try
{
settings = (Hashtable)formatter.Deserialize(stream);
}
catch
{
}
}

if (settings == null) return new Hashtable();
stream.Close();
isoFile.Close();
return settings;
}


--

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved.


"Stacy" <sod...@poboxnospam.com> wrote in message
news:envsu8C#BHA.2040@tkmsftngp05...

Tom

unread,
May 14, 2002, 2:23:33 AM5/14/02
to
> Another way to accomplish this is with something called IsolatedStorage.
> Basically the steps are this:

Is there a benefit to using this, as opposed to using app.config? (I'm
somewhat slow, so I just discovered app.config yesterday and it seems on
topic to me :-))

Tom


[MVP] Thomas Tomiczek

unread,
May 14, 2002, 2:53:20 AM5/14/02
to
Full Freedom.
The ability to have an isolated storage area per user.
The ability to use isolated storage as file space - like for storing more
and binary data.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"Tom" <n...@spam.plz> wrote in message news:uAXZh$w#BHA.1984@tkmsftngp04...

0 new messages