Application Parameter Storing - Best Practices

G

Guest

Hi!
I am new to Windows Forms, but battle-scarred in WIN32 API Windows. I am
developing a forms application that needs to save the users settings (DB
settings, profile selections, etc). I am looking for the best place/way to
store these settings by user.

The appconfig protocol prevents the application from updating the config,
and this is probably not the best place to keep user data.

Is the best practice to use the registry? Or, are folks using some other
place to keep these user settings?

Thank you for your help.
 
J

Jeff Gaines

Hi!
I am new to Windows Forms, but battle-scarred in WIN32 API Windows.
I am developing a forms application that needs to save the users
settings (DB settings, profile selections, etc). I am looking for
the best place/way to store these settings by user.

The appconfig protocol prevents the application from updating the
config, and this is probably not the best place to keep user data.

Is the best practice to use the registry? Or, are folks using some
other place to keep these user settings?

Thank you for your help.

I will be on the lookout for any 'official' answer but I was under the
impression that MS recommended a couple of years ago that app specific
settings were best kept in app config files rather than the registry. I
read this in a Delphi book but I've never been able to confirm it.

I still use ini files but I guess I ought to move to XML files.

An XML config file in the app's directory would probably work and save
the registry from bloat.
 
G

Guest

Hi, Jeff!
I agree with you about using the config file. However, I ran into a snag
that forced me to look in other directions. The ConfigurationSettings object
does not seem to allow UPDATE of settings (maybe I missed something). So, if
the user wants to update one of these parameters, I can't use the
ConfigurationSettings object to modify the config.xml file. I can't see me
asking the user to manually modify this config file; that just seems
non-friendly to me.

Have you had a different experience with the ConfigurationSettings
namespace? If so, please share. Thanks, by the way!
 
J

Jeff Gaines

Hi, Jeff!
I agree with you about using the config file. However, I ran into a
snag that forced me to look in other directions. The
ConfigurationSettings object does not seem to allow UPDATE of
settings (maybe I missed something). So, if the user wants to update
one of these parameters, I can't use the ConfigurationSettings object
to modify the config.xml file. I can't see me asking the user to
manually modify this config file; that just seems non-friendly to me.

Have you had a different experience with the ConfigurationSettings
namespace? If so, please share. Thanks, by the way!

I am ashamed to say I have never heard of the ConfigurationSettings
object, let alone used it!

What I did with one app was to serialise a class that contained the app
settings then read it in/out on startup/shutdown/change. It worked
fine, but I still like ini files :)

I will have to read the help on ConfigurationSettings!
 
T

Tom Dacon

I'd cast my vote for XML format. The registry is deprecated as a location
for user preferences these days, for a variety of reasons. The .Net
framework supplies no managed classes for ini file access, so you have to
downshift to the Windows API; furthermore, ini files have only a single
level of hierarchy, whereas user preferences generally benefit from a deeper
hierarchical organization.

For location, consider investigating isolated storage (namespace
System.IO.IsolatedStorage). Depending on the options you use with it, it can
be isolated to individual users, and provides a semi-hidden location in the
file system which is, in essence, an encapsulated file system in its own
right. In it you can store anything that can be put in a file system (think
XML configuration file). If you're worried about your users going into their
preferences files with notepad and screwing them up (easy to do with XML),
isolated storage hides them from everyone except the most sophisticated
users. If you don't want to go to that much trouble, you can also simply use
Application.UserAppDataPath or Application.LocalUserAppDataPath as the
directory in which you site the XML configuration file. Don't use the
executable's directory, since you can't guarantee that a system
administrator on the customer's site won't lock down the directory for
security reasons. XmlDocument (namespace System.Xml) is probably the best
choice for loading, updating, and saving the preferences.

As you've noted, Microsoft considers the application configuration file to
be reserved for read-only application-wide settings (modified with notepad
only when required for tracing, debugging, etc.), and consequently it's a
poor place to put user preferences. It's also subject to the problem that
the directory might be locked down for security reasons. Even with that
said, I've seen at least one Microsoft-supplied sample in which the sample
writer supplied update capabilities for that file, presumably on the
assumption that it will be run only on a developer's machine that has only a
single user account, and possibly because the sample writer didn't want to
take the time to write the code for separate user preferences files.

If you go to www.gotdotnet.com, you can probably find at least a dozen user
contributions that manage updatable application configuration files,
individual XML user preference files, registry access for user preferences,
and even ini files. Quality varies. The code that you have to write around
XmlDocument is kind of intricate in a few places, mostly around adding
attributes to elements and adding nodes when the parent node path doesn't
yet exist, so it might be worth taking a look at someone else's solution of
the problem even if you prefer to write your own. Like a lot of other
people, I suppose, I've written my own code for XML configuration files, but
when I looked around at gotdotnet with the idea of contributing it I decided
that the world didn't need yet another one, no matter how excellent :)

HTH,
Tom Dacon
Dacon Software Consulting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top