Just now I came across ApplicationSettings in .NET WinForms that could handle complex types.
Currently I am using AppSettings in my ASP.NET WebForms which can handle only string.
Can I use ApplicationSettings in Webforms? If so how?
-
I think what you are asking is "Are you able to Store More Complex Types in a similar way to AppSettings inside a Web App ? If so, then you need to have a look at "Embedded Resources" rather than storing them in Web.Config – Dave Long Jun 09 '11 at 07:32
-
@Dave: http://stackoverflow.com/questions/6288943/web-config-appsettings-complex-values/6288986#62889986 triggered this question. read the comments. i am stil skeptical – naveen Jun 09 '11 at 07:35
2 Answers
The basic idea:
In a different project, create classes that will hold your custom settings. For example:
public class EndPoint { public string HostName { get; set; } public int Port { get; set; } } public class EndPointCollection : Collection<EndPoint> { }
Build the project containing the classes.
Go to the Settings tab in Project Properties. It will say that there is no settings file yet and ask if you want to create it.
Add a new settings file. In the type field select Browse and type the full class name. For example:
ClassLibrary.EndPointCollection
. Save and rebuild the project.Hit the edit button for the setting value. (Note that this will not be available if the classes made in the earlier step are in the same project.) Use the UI to edit the settings.
If you open the web.config / app.config file, you will see something like this:
... <applicationSettings> <WebApplication1.Properties.Settings> <setting name="MyEndPoints" serializeAs="Xml"> <value> <ArrayOfEndPoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <EndPoint> <HostName>MyHostName</HostName> <Port>12345</Port> </EndPoint> <EndPoint> <HostName>MyHost1</HostName> <Port>1212</Port> </EndPoint> </ArrayOfEndPoint> </value> </setting> </WebApplication1.Properties.Settings> </applicationSettings> ...
Finally, to read these settings from your code, simply use
var endPointCollection = Settings.Default.MyEndPoints;
The designer will have created, behind the scenes, the strongly-typed objects to allow the above to work. You can see the full details in the
Settings.Designer.cs
file.
Bottom line: you can make all kinds of custom type settings, as long as those settings have XmlSerializable or have type converter. This technique works on Web Applications, WinForms, WPF, Console Applications etc.
-
6In case anyone else isn't seeing the editor `...` button the reason may be that the class must be in a different project than where the settings is located. That is how the answer is structured but it didn't make that point clearly. It's a "known limitation" according to: http://msdn.microsoft.com/en-us/library/wabtadw6(v=vs.100).aspx – Austin Thompson Oct 01 '12 at 13:59
-
For those who are skeptical with the proposed answer, I would recommend to check the following question/answer: http://stackoverflow.com/questions/204695/storing-values-in-the-web-config-appsettings-or-configsection-which-is-more ... which comes up with the idea of creating a custom configuration section – Learner May 16 '12 at 14:15
-
2@AustinThompson, I think that technically, the class must be in a different *namespace* to the project; it still seems to work if it's in the same project but with a different namespace. – Sam Jun 26 '13 at 12:16
-
4Having just tried to implement this solution, I can never seem to get the setting editor regardless of if the class is in a separate project or a different namespace in the same project. Does this solution still work in VS2012 & .Net 4.5? Are there additional steps not mentioned? – philreed Oct 24 '13 at 11:17
-
I successfully went through these steps, and added a few settings using the editor. however i don't see the serialized settings in my web.config. Actually, it appears that after hitting "ok" in the editor wizard, the settings don't actually get saved. there is no "save" and doing "save all" in vs, seems to just reset the setting – Sonic Soul Mar 23 '14 at 19:42
-
seems like a lot of hoops to jump through just to get that serialization to work.. going to just serialize to mySettings.json and json is a lot easier to manage then xml. and can skip that partially working wizard nonsense – Sonic Soul Mar 23 '14 at 23:29
-
I've been looking for this for a long time. Works great. Thank you! – Andrei Krasutski Mar 06 '18 at 20:17
Here's a variation on the accepted answer, using the following user-defined class to represent a setting:
namespace MyApplication
{
public class EndPoint
{
public string HostName { get; set; }
public int Port { get; set; }
}
}
The accepted answer proposes the use of a specialised collection class, EndPointCollection
to hold settings. However, I don't think this is necessary; an array type (EndPoint[]
) also seems to work.
However, typing the array type in the type browser doesn't work; you can instead specify the type directly in the .settings file (using a text editor):
<Setting Name="MyEndPoints" Type="MyApplication.EndPoint[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
Also, if the value editor shown in the accepted answer isn't available, you can instead type the values directly into the value field using XML:
<ArrayOfEndPoint>
<EndPoint>
<HostName>MyHostName</HostName>
<Port>12345</Port>
</EndPoint>
<EndPoint>
<HostName>MyHost1</HostName>
<Port>1212</Port>
</EndPoint>
</ArrayOfEndPoint>
Note that the XML namespace declarations that Visual Studio generates aren't necessary in the XML, as shown above.

- 40,644
- 36
- 176
- 219
-
1Note also that the above doesn't work for members of the serialized type which are read-only (private property setter or `readonly` field). Which unfortunately means that for a lot of kinds of user-defined types someone would want to store in the settings -- e.g. simple immutable value-storage types -- this very useful technique cannot be used. :( – Peter Duniho Jul 25 '15 at 00:22