views:

9073

answers:

5

What I want to achieve is very simple: I have a WinForms (.NET 3.5) application that uses a path for reading information. This path can be modified by the user, by using the Options form I provide.

Now, I want to save the path value to a file for later use. This would be one of the many settings saved to this file. This file would sit directly in the application folder.

I understand three options are available:

  • ConfigurationSettings file (appname.exe.config)
  • Registry
  • Custom XML file

I read that the .NET config file is not foreseen for saving values back to it. As for the registry, I would like to get as far away from it as possible.
Does this mean that I should use a custom XML file to save configuration settings? If so, I would like to see code example of that (C#).

I have seen other discussions on this subject, but it is still not clear to me.
Thanks!

+8  A: 

The registry is a no-go. You're not sure whether the user which uses your application, has sufficient rights to write to the registry.

You can use the app.config file to save application-level settings (that are the same for each user who uses your application.

I would store user-specific settings in an xml file, which would be saved in Isolated Storage or in the SpecialFolder.ApplicationData directory.

Next to that, as from .NET 2.0, it is possible to store values back to the app.config file.

Frederik Gheysels
Yes. Never use registry if you have a choice. +1
Kb
Use the registry, though, if you want per-login/user settings.
thenonhacker
Registry is non-portble
Kb
@thenonhacker: Or use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Nazadus
A: 

As far as I can tell, .Net does support persisting settings using the built-in application settings facility:

The Application Settings feature of Windows Forms makes it easy to create, store, and maintain custom application and user preferences on the client computer. With Windows Forms application settings, you can store not only application data such as database connection strings, but also user-specific data, such as user application preferences. Using Visual Studio or custom managed code, you can create new settings, read them from and write them to disk, bind them to properties on your forms, and validate settings data prior to loading and saving. - http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx

Jacob
Not true.. see aku's answer above. its possible using Settings and ApplicationSettingsBase
Gishu
Ah, I see... My bad
Jacob
+32  A: 

If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer, choose Properties. Select Settings tab, click on hyperlink if settings doesn't exist. Use Settings tab to create application settings. Visual Studio creates Settings.settings and Settings.Designer.settings files that contain singleton class Settings inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:

Settings.Default["SomeProperty"] = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration file

This technique is applicable both for Console, WinForms and other project types.

Note that you need to set scope property of your settings. If you select Application scope then Settings.Default.< your property > will be read-only.

aku
+5  A: 

The ApplicationSettings class doesn't support saving settings to the app.config file. That's very much by design, apps that run with a properly secured user account (think Vista UAC) do not have write access to the program's installation folder.

You can fight the system with the ConfigurationManager class. But the trivial workaround is to go into the Settings designer and change the setting's scope to User. If that causes hardships (say, the setting is relevant to every user), you should put your Options feature in a separate program so you can ask for the privilege elevation prompt. Or forego using a setting.

Hans Passant
Could you please expand upon your last sentence? Ask for elevation to write app.config or to write a separate application that would go through all users' home folers, look for user.config and edit these?
CannibalSmith
The separate program requires a manifest to ask for elevation. Google 'asinvoker requireadministrator' to find the proper syntax. Editing user.config is not practical, nor necessary.
Hans Passant
A: 

I don't like the proposed solution of using web.config or app.config try reading your own xml. Have a look at:

http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx

Thanks

gatapia