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

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

Related

How do I make changes to AppConfig userSettings?

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.

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.

How to change a website default document programmatically on the fly

Using ASP.NET 4.0, IIS 7.5.
I have a website engine, I have just implemented a way for this to tell if it's being loaded on mobile and instead of loading Controls\MyControl.ascx it loads Mobile\Controls\MyControl.ascx. This works well for my controls and also my MasterPage.Master file.
What I can't figure out however is how I can do the same with Default.aspx. This needs to be done on the fly programatically as I need to be able to check if it's mobile version. I was thinking of doing something on a pre-init event in globals but not sure if that's the best way.
Note: I don't want to use inline code on Default.aspx and just display different content base on my Mobile flag as my scenario goes one step further by basing the file on customer as well and this would mean having one huge Default.aspx for all customers which wouldn't be manageable.
Changing the default document on the fly is not possible in any practical sense.
Writing to the web.config on the fly to load a mobile version of a default page is quite frankly terrible and not an answer to the true context of your issue. I would feel irresponsible as a developer if I even proposed this as an answer to loading a mobile version of a default page.
I was trying to help you solve your problem and not just answer the base question in the title. As we all know, changing the web.config will restart your application and would not serve as a true solution, as you could not do this and achieve any kind of performance.
Here is the BEST alternative (IMHO) to dealing with mobile browsers.
http://51degrees.codeplex.com/
HTH!
For anyone else looking at this I have found a solution but I am not sure I will implement it as I don't like the idea of updating the web.config file at run time. Using the Microsoft.Web.Administration namespace you can update the server.webServer -> defaultDocument section programmatically. Doing this allows you to change the path to default.aspx and it will load based on the variables you set.
This link should provide more information: http://blogs.msdn.com/b/saurabh_singh/archive/2007/11/24/accessing-iis-7-0-features-programmatically-from-configuration-file-s-c.aspx
Also, the Microsoft.Web.Administration dll isn't available directly in VS so you need to add it from %windir%\syswow64\inetsrv (64bit version).

Datasources in C#

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.

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