Using ApplicationSettings with F#
While F# is a first-class .NET 4 citizen it’s not really a first-class Visual Studio citizen, even in 2010. A lot of the application setup tools we have for C# aren’t present in F#.
I was recently making a quick and dirty tool in F# that needed some basic user settings, so I went with the ApplicationSettings infrastructure in .NET, as it’s the easiest way to implement this. In C# this is easy, the GUI designer writes the code for you. In F# you have to do it yourself. Microsoft’s documentation is in C#, so there’s some adaptation to do.
You’ll need a type that inherits
ApplicationSettingsBase class from
System.Configuration, and then add some property members with the attribute
UserScopedSettingAttribute(). You can also use
for application-level settings, but
these require editing the XML in your App.Config, something that I’m not that
familiar with yet.
There’s a little boilerplate for each setting, but other than that it’s a breeze.
Here’s a sample settings class:
open System.Configuration type MySettings() = inherit ApplicationSettingsBase() [<UserScopedSettingAttribute()>] member this.UserSetting with get() = this.Item("UserSetting") :?> string and set(value : string) = this.Item("UserSetting") <- value [<ApplicationScopedSettingAttribute()>] member this.AppSetting with get() = this.Item("AppSetting") :?> string