How do I make changes to AppConfig userSettings? - c#

So I was having trouble adding a new App.Config userSettings. Without any knowledge. I tried to set the configuration directly in the app.config HML. Files get overwritten. It stays in App.Config but it does not show up in the Settings.Designer or IntelliSense. I know it should show up in the
IntelliSense because the configured userSettings that are already present do.
How do you correctly configure a user setting?
Maybe, this question is a duplicate of this: How to save application settings in a Windows Forms Application?
I guess I would accept that, but it was difficult to find the answer to the question starting from the point of view I started with. I think that there are allot of well meaning souls like me that try to directly edit the app.config file to achieve this and wonder why they get nowhere fast. These askers are are unsure where to find the correct answer. I also suspect that other well meaning souls that answer their questions assuming that the questioner was asking from a prospective where they knew the answer to this question. This of course makes understanding difficult to reach for the original seeker of knowledge.

Answer: I checked Microsoft to learn more and found this link:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-a-new-setting-at-design-time
which says this:
Open Visual Studio.
In Solution Explorer, right-click your project node and choose Properties.
In the Properties page, select the Settings tab.
In the Settings designer, set the Name, Value, Type, and Scope for your setting. Each row represents a single setting.
I found that I could even use custom types like my enum, which vs will convert from a string in the HML.
Now this procedure populates App.config and Settings.Designer.cs with the setting for you. It shows up in IntelliSense too. Fantastic!
Note: The settings can also be accessed by Settings.Settings.
Now I have noticed, since I continually seem to want to get under the hood and do things in an undocumented way: It is possible to directly edit the Settings.Settings file. Don't do that either, That will get you pretty far but the settings you configure there will not persist when you build.

Related

Update app.config file after installer upgrade

I'm currently working on a Microsft Word Application-Level Add-in using C#. My application contains an app.config file. In this file I save user-settings (userSettings-Section) and some data defined by a custom ConfigurationSection. The data stored inside of custom ConfigurationSection is also user-specific.
I access the user-settings as follows:
Properties.Settings.Default.MyUserSetting
The custom ConfigurationSection I'm using like:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
.GetSection("MyCustomConfigurationSection")
My questions are:
Do I even use the app.config the "correct" way? (I know that there is probably not only one correct way, but is "my way" one of these?)
Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
And Most importantly: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
I deploy my Add-in using an installer built with WiX. At the moment I use CustomActions, which manually insert code for each added/removed/changed setting. But I was wondering whether there is a built-in way of doing this.
I did some researches the whole day now and I think I can answer my questions for myself:
Q: Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
A: It seems that Propeties.Settings.Default use ConfigurationUserLevel.PerUserRoamingAndLocal, what sounds logical as they only contain userSettings... I don't know whether it is possible to change it, but for me it is not necessary.
Q: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
A: I found out that there is an Upgrade()-Method in Settings which should take care of copying user settings between an older version of the product and the new one. For more information see http://ngpixel.com/2011/05/05/c-keep-user-settings-between-versions/. I must confess that I haven't had occasion to test it, since I have no permissions to build the installer, but I will rely on it. I will come back to this post after I tested my solution.
Furthermore I am not using a custom ConfigurationSection any more. Instead I derive from System.Configuration.ApplicationSettingsBase, which means that I am able to handle the custom data exactly like the Properties.Settings, including the Upgrade()-Method. See this post for more information about how to use it.
Hope this helps somebody.

Is there a non-default alternative for Properties.Settings.Default?

In ASP.NET, when you want to reference application settings, you use the Properties.Settings object. Immediately after that, there is a Default object which contains all the application settings.
My question is: Is there a way to have something other than that Default profile in Properties.Settings.Default? Something like a custom non-default area for settings. I have a web application that has lots of modules and each one has a bunch of settings relevant only to that module. I'm looking for a way to keep them separated.
The closest thing I've found is this SO question (Using Properties.Settings.Default and Setting Alternate Profiles?), but it wasn't really the answer I expected and I wanted to give it another shot and see if anyone had discovered something new.
Edit: This MSDN document explains EXACTLY what I want (http://msdn.microsoft.com/en-us/library/bb397748%28v=vs.110%29.aspx). However it's just not working for me. The supposed Settings File type doesn't exist.
Could you create extra web.config files and use the WebConfigurationManager to read from the appropriate config file?
Edit: Sorry, I had originally said CongurationManager you will want WebConfigurationManager.
Assuming you meant "I want to create my own section" (of settings)
see How To create Custom Config Sections.
if you choose, you can it further and create separate MyCustomSettings.config files , e.g. <pages configSource="pages.config"/> see configSource

Visual Studio 2012 application settings

I have read the tutorial on application settings, and I don't understand a bit. All I know is that I've created a .settings file using the designer, assigned values to it, and Properties.Settings.Default doesn't let me access them because it says they don't exist (what the tutorial is saying is that if you create an entry called Foo, then you can access it with Properties.Settings.Default.Foo, which, as I said, doesn't work). Can someone please explain to me how to work with .settings files and access the settings themselves?
I had the same problem, but then I used only Settings.Default instead of Properties.Settings.Default, and it works now.
Please, set select Access Modifier as internal and I believe it will help.

How to avoid all code nodes to expand on project opening?

I have a C# project and some code pages have few thousand lines of code. I really like the idea of nodes in the code editor. I use it a lot and create many regions. But every time I open the project, all the nodes are expanded and I have to minimize them manually. It gets really annoying.
I have not found any help about this on internet nor in the options of VS. There must be a setting somewhere.
In the Options dialog onder Text Editor --> C# --> Advanced, there is an option "Enter outlining mode when files open". This should be checked.
In the IDE, from the Tools menu click Options. Alter default Outlining using
TextEditor->C#->Formatting->Advanced
There is a checkbox here you can use to set defaults for Outlining. This is VC# 10 Express but similar in other versions.
If you try the suggested change, you may be disappointed. According to this C# PM, who's responding to a similar complaint:
It is a bit confusing, but the
behavior you're seeing is intended.
The feedback that we received with VS
2003 was that we should persist the
outlining state of source files after
they have been closed and then
reopened. The option in Tools |
Option now effectively means what the
default behavior should be for a file
that you have never opened before. It
has no effect on files that you have
opened previously, since those files
already have a persisted outlining
state.
I don't want be a smart ass here, but often if you have that huge code in one file, you have more than one logical unit and might be able to (ans should) split it. (Single Responsibility Principle).
For my share, I don't like the regions because they are hiding code and I prefer to see all of it.

What is the best way for save and load setting in my program?

I have build a C# program.
I need to keep setting of my program and I need to load them when the program load
(I know to do it with simple text file....but i don't have good documentation of the variable)
what is the best way to do it ?
Can I get any sample ?
In your solution, if you right click on the project and click on properties, there's a settings tab. You can define the settings you want to track and their types there and then access them through code, like this:
Properties.Settings.Default.SettingName = "Test Value";
Properties.Settings.Default.Save();
And then on load:
textBox.Text = Properties.Settings.Default.SettingName;
I'd argue that just hinting towards appSettings doesn't really cover the topic, as there is more advanced and eventually easier to use stuff readily available. Given that this question might be a
Duplicate
Best way to save per user options in C#
Please note that the MSDN link provided there (User settings in C#) covers both user and application scoped settings on an easy to grasp introductory level and basically elaborates on the correct example provided by jasonh already.
For a much deeper coverage of these topics (assuming you are using Windows Forms or WPF with Visual Studio) I'd recommend to look into Application Settings for Windows Forms.
Use appSettings (that's the MSDN link, here's more of a quick overview).
How about storing the settings in XML?
Reading and writing them is pretty easy.

Categories