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.
Related
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.
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
I am wondering if .net has some sort of inbuilt gui for editing application.config files.
From googling around it seems that they exist but are designed to be run from visual studio.
What I want to do is to allow admins of the software I am writing to be able to customise the application config from within the software at a customers site.
Could someone advise me if this is possible?
At the minute I thinking I will have to wite my own code to parse the config files to generate the Gui.
Create your own form whcih takes application.config values from user and update that in the application.config
Long time reader, first time poster.
Background:
We have developed a small application in C# for creating proprietary binary files for use in our embedded system. The application is very simple, just a few textboxes, comboboxes and checkboxes, and a couple of buttons for creating files. It is distributed to our customers as just an .exe file, meaning, no installation is required, and that is how we want it.
Problem:
I have been tasked by our support staff to implement a way to save some settings and load them again. This data should be retained in between runs of the app. The user should be able to save a couple of these under different names.
The question is:
What are my options? So far I have identified 3 possible solutions:
Plain text files: Cumbersome to work with
Settings file: Visual Studio can use a settings file as a datasource, I have tried this approach, but is stuck. Perhaps not for me?
Microsoft SQL Server Compact: I know very little of this. Can we use our deployment method with this solution?
You could use an Xml file to save your settings. Your application could load these settings and act accordingly. If you are .Net 4.0, you could LINQ that will make your job pretty easy. Have a look at this.
If the data to be saved is small and simple, then I would go for physical files; the overhead in setting up the SQL Server Compact for you application is larger than just serialising a class or struct to XML and saving to file.
On the other hand, if the data becomes more complicated, including one-to-many or many-to-many style of relationship, then you would want to move to the database solution.
To keep flexibility ensure you decouple the save/load code so you can change from file to database if the need arises.
"C# Settings" should work for you. Have you tried it right?
Please see the below links. Hope it helps
Visual Studio Settings file - how does it work?
http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx
It worked for me.
The easiest way is to use user or application settings
Add an app.config file to your project and edit the settings like this
<setting name="Setting" serializeAs="String">
<value>This is the setting value</value>
</setting>
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx#settingscs_topic2
In the past, I have created a small "settings" class.
I have then over ridden the ToString method and also created a parse method.
I have stored each setting in the settings file using a global::System.Collections.Specialized.StringCollection type to store a collection of settings. I use the ToString method to store the setting and the Parse method to read it back in.
Lastly, I use an int type to store the currently selected index.
I am usually using Setup and Deployment template provided by visual studio. Is it possible to enhance default forms provided in that. or is there anything else to create the customized and attractive installer.
Aim :
1. I like to ask the user for pre-requested information to initialize the application during the installation process.
2. I like to prevent app.config by toching from user.
3. I like verify the collect the connection information during installation as well as like to verify that by connecting with given data source.
please provide good direction if i am in wrong path. - thanks
Wix is what you need
Yes, you can do all these things with the built in setup project in Visual Studio.
You can add a new user interface dialog box with text boxes, check boxes etc.
Write a custom action the handles these values and store them in some configuration file.
The app.config shouldn't be touched by the user anyway. If you are using the built in configuration classes in .NET, the user settings will be stored in a separated file in the user's application data folders.
As in #1, you can use custom actions for this kind of functionality.
The built in setup project has very little/no means of customizing the look and feel. For that you will have to use either 3rd party components/installers or use WiX and design setup screens/dialogs by yourself.
I use Inno Setup (http://www.innosetup.com/isinfo.php), its free and very configurable, it can also run your own applications as helpers to do very specific tasks.
Why not try other install systems like InstallShield or, if you really want to customize go for NSIS (Nullsoft Scriptable Install System)? NSIS Download