Google グループは Usenet の新規の投稿と購読のサポートを終了しました。過去のコンテンツは引き続き閲覧できます。
Dismiss

Storing App Settings

閲覧: 5 回
最初の未読メッセージにスキップ

Andrew

未読、
2002/05/10 8:38:142002/05/10
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

未読、
2002/05/10 10:26:382002/05/10
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]

未読、
2002/05/13 14:06:122002/05/13
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

未読、
2002/05/14 2:23:332002/05/14
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

未読、
2002/05/14 2:53:202002/05/14
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 件