UserScopedSettingAttribute or ApplicationScopedSettingAttribute? - c#

I'm implementing a new class library's settings and I'm wanting to use .NET 2.0's settings architecture, instead of the regular appSettings section in a .config file.
I've created a Main.settings file through the Visual Studio 2008 IDE, and this has autogenerated both the Main.settings file and a corresponding Main.Designer.cs file. In the IDE, I have to select between whether each setting should have User scope or Application scope, which translates to the IDE applying either UserScopedSettingAttribute or ApplicationScopedSettingAttribute to the setting's Property.
The stuff on MSDN I've found on the topic seems to come at things from the point of view of a Windows Forms application; it talks about using user-scoped settings for stuff which pertains just to the user using the app, and application-scoped settings for stuff which always pertains to the app, no matter which user is using it.
However, what should I do when my settings file isn't for a Windows Forms app, but for a class library that I'm calling from a website under IIS? Does it matter whether I scope the settings as User or Application? If so, what factors are there to take into account as to how I should scope the settings?

Web applications do not support user scoped application settings. To get similar functionality, you could use the SettingsProvider class or create your own implementation of it. For application-wide settings, you will have to use the web.config file and either
Utilize the appSettings section and use System.Configuration.ConfigurationManager to access those settings, or
Implement your own ConfigurationSection.
The answer to your second question is: it depends.
In a web app, user-scoped settings are usually associated with a session and allow for each user to have a different value for a particular setting. These settings are usually used to customize a user's experience. On the other hand, if you want a setting to be the same application-wide regardless of the user who is accessing the application, you should be using an application-scoped setting.
If you provide more detail regarding your specific situation, I may be able to provide further guidance.
Also, take a look at this question if you want to use a config file from the class library instead of placing your settings in the web.config file.

Related

writing User configuration back to app.config in .net application

I have a windows application. I am writing all user specific settings into user.config.
Now in a new scenario, whatever changes one user does it should reflect to all the users.
For that purpose I thought of writing the settings into app.config through
Properties.Settings.Default.p1 = value;
Properties.Settings.Default.Save();
And every user read the updated default value after
Reset();
But what ever I do…it still writes to user.config not to app.config
From my experience, the Settings class cannot be used to modify default setting values for all users. In this situation, I modify the app.config directly using the classes in the System.Xml namespace. It is not ideal, but I have not found any other way to do it.
There are a couple things to keep in mind when modifying your app.config file:
If your program is installed in the Program Files folder, you may need to run your application as an administrator in order to save changes to the file. It will depend on how your security is configured.
If you change the namespace of your Settings class, it will change the structure of your app.config file, so you would need to update your code to account for the new XML structure.
See this MSDN article for more information about application and user scoped settings:
http://msdn.microsoft.com/en-us/library/8eyb2ct1%28v=vs.110%29.aspx

WPF Properties.Settings saving and multiple users

In my applications there are some setting saved in the Properties.Settings.Default. These settings can be changed by the user(s) and needs to be saved locally on the computer. While I can save these setting, the problem is that it is only saved for the user currently logged in. Once an user changes a setting it has to be for all users of the computer. How can I accomplish this?
User scoped settings are just that, settings that an individual user can change and will only be saved for that user.
The application scoped settings will affect all users but they are not designed to be changed by a user.
You might want to consider a different approach to storing settings that you want users to be able to change but to affect all users of an application e.g. the Windows registry or an external xml file.
Another option is to use user scoped settings but to change the location to a centralised location so that all users use/save the same settings. See Store user settings into application folder for an option on how to do this.
When you open the Settings Designer window in Visual Studio, you have four values that you need to enter for each setting:
You need to set the Scope property to Application to have a setting that is the same for all users. For the full story, read the Using Application Settings and User Settings page on MSDN.
Application settings cannot be changed, only by hand before the application is run so I do not recommend that approach.
In my opinion, propagating the changes may be generally a bad approach. Since this config (user.config) is generally stored in the user's own folder (under Users), it should not be modified by another user (in fact, without administrator acces, another user cannot even access).
I might recommend using other places to store application specific settings: xml or config file near your application, or maybe the registry.
I would use an external Database for that Stuff...
But if you want it quick and simple just save it to a File on the Harddrive (for example C:\Program_Data\\settings.csv) i would use a csv file because it's not much work...

How to go about creating a configuration file for my application in .NET

I am working on a software and want to create a configuration file for my application to store configurations that will be used at runtime. I have seen some software use a config.xml file to achieve this. The configuration file I want to use will have:
Cache folder location
Color scheme
Option to toggle caching
How to go about this? I am working with WPF.
In Visual Studio, add an "app.config" to your project. When you build your application, this will create an AppName.exe.config file, where AppName is the name of your executable. This is an XML config file that can contain your settings.
If theses options are user-specific, don't use app.config - that's for global application settings and you will clobber other users' settings. I'm bringing this point up because you mentioned caching options in your question, and in some applications, the choice to cache or not to cache is at the user level.
Instead, I would recommend that you create a domain object to store the configuration settings to, and serialize the object to a local folder in the current user's isolated storage folder.
When the application starts up, just look into the current user's isolated storage folder, confirm the serialized file exists, and deserialize back to the domain object.
as you are in WPF world, you also can use App.XAML for defining and using color schemes
Use standard .NET settings framework.
More details here: How is the logic behind storing program setting in text file like 'config.cfg'?
Also this will help http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx.
Although this article is a little old, not much changed since then in this realm.

Add User Settings at runtime in a c# application

I want to add new settings to the user.config at runtime for a C# application (WPF).
These settings will be added by independent modules so I have no idea what they will be in advance.
Most examples refer to:
Configuration config =
configurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
But this only allows you to access the Application Settings
and you can't save it anyway as it's not meant to be modified.
I want to be able to do something like
Settings.Default.Add("SomeKey", "someobject");
Settings.Default.Save()
after which this setting would be available the next time this specific user
starts the application.
You may wish to check this solution.
It is doable but it is lots of code.

.net desktop app - how do store/change application settings or connection strings?

I need to give the user the ability to change application settings, in this case the location for the application database. I noticed that Application Settings are read only at run time, but this needs to be application-wide, not user-specific. How do I persist an application-wide connection string in windows.forms that is changeable at runtime?
You can use
ConfigurationManager.AppSettings.Set()
Also
ConnectionStringSettings.ConnectionString Property
Can be set too.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("test","tada");
config.Save(ConfigurationSaveMode.Minimal, true);
There isnt a direct way in the framework using the newer Settings classes - you can save user-level settings with the Settings support, but not application/machine level. Easiest way is to XmlSerialize a class and store it in a directory that is shared between all users with write acccess when they are running un-elevated such as the Public Documents folder (the program directory shouldnt be written to), e.g.. Environment.SpecialFolder.CommonApplicationData
EDIT: Yes, the old appSettings mechanism has a way of writing, but that's bad news - the newer Settings stuff omits this facility as it is bad news for lots of reasons to try to write to config files in Program Files

Categories