Add User Settings at runtime in a c# application - c#

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.

Related

How to import a Winforms settings file at run time

I am the dev of a c# winforms app which contains a lot of settings. These settings get tweaked and optimized by a domain specialist user/tester.
The user is able to send me his app.config file from time to time and I would like to load and activate his settings at run time (even better, selected ones) on my side in one fell swoop, given that I have his app.config file to hand. In other words I want to code an 'Import settings' option.
I am aware I can just overwrite my own app.config with his, but surely I would have to do this prior to starting my app?
Any help appreciated.
After you replace the existing current App.Config with the updated one.
You can open the current configuration in code and read the current value like below
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MyUpdatedValue = configuration.AppSettings.Settings["myUpdatedValue"].Value as ...

C# settings hidden for user

I want to get and set some application settings hidden for the user. In this case these are two tokens and two tokens that are being altered by the application. But still all four them are a secret to the application user.
I found two ways on MSDN and this forum.
Use a .config file. -> this sounds okay and I can change some values in it. But I see still a plain text file after publish.
Use the settings in the project properties. -> should it be? I read warnings about that this not so secure after all.
A lot of sites describe a web server solution while I just want to have a desktop solution.
Btw. It is not a commercial product, I am making a hobby project to learn more from C#.net.
If these files need to be altered by the application, you will need to persist these outside of the application so that the new settings will be loaded next time the app starts.
For this, you can always create a config (or settings) file and hide it in the System.IO.IsolatedStorage files for the assembly. This is still editable by the user, but its a bit of a pain to find - plus this will transfer over with roaming profiles so that the settings are used cross machine.
Something simple to help you start out:
IsolatedStorage.IsolatedStorageFile storage = IsolatedStorage.IsolatedStorageFile.GetUserStoreForAssembly();
string subdirectory = "SomeDirectory";
storage.CreateFile(System.IO.Path.Combine(subdirectory, "settings.config"));
Does the * existence* of the setting need to be kept secret, or just its value?
If the latter, I'd suggest encrypting the value you save to the .config file.

how to change settings every time when I create setup

I have developed a wpf application for a client and I dont want to give the source code. Since the client wants to change the logo and few links in the application for his different set of users, how can I give a functionality for him to create a setup with different settings each time?
I thought of giving the debug folder so that he can change the exe.config and create the setup with Inno setup. is this the right way or can you please guide me another solution?
Thanks,
Venkz
Why don't you create an "app.config" (application configuration) file? It's just a simple XML file with defined syntaxes or you can define your own configuration settings.
For more info about this, see this on MSDN:
http://msdn.microsoft.com/en-us/library/kza1yk3a.aspx

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.

Configuration file with ClickOnce deployment

I've been trying to modify my application to deploy and update using ClickOnce. I've managed to get the program working but I'm having trouble with the program configuration. My program uses a custom XML configuration file located in the application directory. This raises 2 major problems.
1.) The configuration file is very hard to get to. Without knowledge of how ClickOnce works the user will not be able to locate it.
2.) Currently if I change the configuration file ClickOnce automatically "updates" the configuration file to the original version, destroying my configuration.
Ideally I would like it to move the configuration file to another location and create a start menu shortcut to it next to my application. But if I change the program to do this can I still deploy the application using ClickOnce?
Thanks in advance,
Fr33dan
Why don't you put a copy of the configuration in the users app data folder (this can be done on first run) - then have a button in your application which opens it (either externally or in your application)?
You can always store your configuration data in the Application Settings. This won't get overwritten on every ClickOnce change or update (unless you change the Type of the setting). You can then create a simple form to update it. That's the technique many .NET developers use for screensavers.
There are a number of things you can do here to mitigate this as a problem.
Firstly, using what's already there - the configuration data has two parts (excuse me as I'm working from memory) app config and user config. The app config is basically defined when the app is pulled down however the user config is just that - you set up the defaults and then, once set by the application on behalf of the user, it won't be overwritten when the app is updated.
It should be straightforward enough to provide a configuration editor - something as simple as a two column grid would be sufficient with a read only label column and an editable value column (although you're going to be somewhat challenged on validation).
Alternatively, if you're happier with a more traditional configuration, then you need precisely 1 user value and that would be the location for the config file... if you don't know if (or can't find the file) prompt to create, dump your default config to the specified location from a resource within you app and then you've got your config file and away you go.
One project I worked on, we made the app download a configuration file from the server it was deployed from (this was done on each startup to cope with if app was added to the Start Menu and cached). The ClickOnce API gives you the server address.
On another project we just pass a few config values as query strings to the ClickOnce app, these were generated by the Asp.net page that had the link to the app.
This allowed customers to change the config for their site without having to resign etc.
(This does not help with per-user config)

Categories