Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#267674 - 21/10/2005 00:53 Another C# question, how best to save preferences?
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Thus far, I am using the registry to save preferences for my program. However, deep down I don't like this, and would prefer to save settings to a file in the Documents and Settings tree in Windows. The one big thing stopping me so far is the amount of bloat it looks like I would have to add to my program to save and load settings from a file. Am I missing something simple, much like what Cocoa offers? By what I can tell in OS X, as a program, you can say "I want to save setting X" with a single function call similar to SetValue on a RegistryKey. The API then take care of creating an XML file if needed, otherwise it updates the existing file created earlier in the users ~/Library directory.

Top
#267675 - 21/10/2005 07:51 Re: Another C# question, how best to save preferences? [Re: drakino]
andy
carpal tunnel

Registered: 10/06/1999
Posts: 5915
Loc: Wivenhoe, Essex, UK
There isn't anything built into .NET 1.x to do this for you. In .NET 2.0 however they have introduced the concept of Application Settings, which sounds like it is designed to do the same sort of thing as the Cocoa stuff you referred to.

I haven't used the Application Settings yet, so I don't know how good it is.

http://msdn2.microsoft.com/en-us/library/0zszyc6e(en-us,vs.80).aspx

We have just started on a new project and have opted to use VS.NET 2005 and .NET 2.0, as by the time the product ships VS.NET 2005 will have been released. We are using the release candidate of VS.NET and it feels very solid and has lots of great new usability features.
_________________________
Remind me to change my signature to something more interesting someday

Top
#267676 - 21/10/2005 13:26 Re: Another C# question, how best to save preferences? [Re: drakino]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
I remember way back when when doing some research into the Win32 API that the function you use to save stuff to the registry had a flag argument that was, essentially, save-to-registry vs. save-to-file. I assume that's gone now?
_________________________
Bitt Faulk

Top
#267677 - 21/10/2005 15:52 Re: Another C# question, how best to save preferences? [Re: wfaulk]
oliver
addict

Registered: 02/04/2002
Posts: 691
AppSettings exist in 1.1, however you can't update them easily with a single function. However, it's just an XML file, so using the XmlDocument namespace you can load, update, and save the file without too much trouble.


Edited by oliver (21/10/2005 15:53)
_________________________
Oliver mk1 30gb: 129 | mk2a 30gb: 040104126

Top
#267678 - 21/10/2005 16:35 Re: Another C# question, how best to save preferences? [Re: wfaulk]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Quote:
I remember way back when when doing some research into the Win32 API that the function you use to save stuff to the registry had a flag argument that was, essentially, save-to-registry vs. save-to-file. I assume that's gone now?


No. It was never there, sorta-kinda. The function I think you're thinking of is called WritePrivateProfileString, and it's designed for writing to an INI file. You can map writes to particular INI files to the registry (by some settings in the registry).

OTOH, you could be thinking of MFC's CWinApp::WriteProfileString which writes to a file unless you've called CWinApp::SetRegistryKey, in which case it writes to the registry.

But, this is not hugely relevant, because we're talking about .NET, in which, bizarrely, Microsoft left out a configuration API (although, as Andy says, it's in .NET 2.0). Of course, you can call the Win32 API functions via P/Invoke.

Or you could just roll your own. It wouldn't be that hard.
_________________________
-- roger

Top
#267679 - 21/10/2005 16:35 Re: Another C# question, how best to save preferences? [Re: oliver]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
Quote:
AppSettings exist in 1.1, however you can't update them easily with a single function. However, it's just an XML file, so using the XmlDocument namespace you can load, update, and save the file without too much trouble.


Except that ConfigurationSettings.AppSettings doesn't always reload the settings if you change them.

If you do a search on Codeguru, you'll find half a dozen preferences classes. I seem to recall that there's one in MSDN magazine, too.
_________________________
-- roger

Top
#267680 - 21/10/2005 17:14 Re: Another C# question, how best to save preferences? [Re: Roger]
oliver
addict

Registered: 02/04/2002
Posts: 691
Quote:

Except that ConfigurationSettings.AppSettings doesn't always reload the settings if you change them.



That's what I get for guessing, I'm mainly a webdeveloper and I've only written 3 or 4 windows applications with .net (only 1 used the AppSettings), however in the web world when the web.config file gets updated the entire application recycles to load the new settings.
_________________________
Oliver mk1 30gb: 129 | mk2a 30gb: 040104126

Top
#267681 - 29/11/2005 01:56 Re: Another C# question, how best to save preferences? [Re: andy]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Finally got time to put some work into my .Net 2.0 based milestone, and Application Settings works wonderfully. Basically 5 lines of code, and I had the app able to save and restore to the preferences file. Ended up being much easier then I thought, and you can even throw it an array and it knows how to deal with it properly on the XML side.

Just need to throw in some sanity checking on the preferences and it should be good to go, with very little code bloat on my side.

Top