How do I setup the Application Settings by code? - c#

I'm using Visual Studio 2017, and I have a boat load of settings I'm adding. It's kind of a pain in the neck to add them by this Settings.settings table in VS. Is there a better way?

If you have the option of not using the default settings application behavior, or if you can use another bahavior in addition to it, perhaps it may help you:
How to create a hand-made application settings file
In this case, this may help you too:
How to initialize user app data and document path
So you can for example put this settings file in the user app data folder or anywhere you want.

Related

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.

Saving user configuration options

I am creating a winform application in .net 2 with c#. I need to be able to save user configuration data and I am considering using an XML file for this propose. What is the general feeling for saving user configuration data? I have read that it is not in vogue to write to the registry but rather to a file instead. Please write your thoughts.
One option is to add it to settings file. In Visual Studio, go to My Project -> Settings and add it there. You can access it this way: C# - properties.settings.default...; VB - My.Settings...
Adding a setting to settings file stores the value in app config file and auto generates a class property for easy access to the value.
Use the ConfigurationManager class to store the settings.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories