FSharp.Configuration


The AppSettings type provider

This tutorial shows the use of the AppSettings type provider. It allows to access app.config files in a strongly typed way.

Using App.Settings from F# scripts

Create a config file called app.config like this:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test2" value="Some Test Value 5"/>
    <add key="TestInt" value="102"/>
    <add key="TestBool" value="True"/>
    <add key="TestDouble" value="10.01"/>
    <add key="TestDateTime" value="2014-05-18 11:14:28Z"/>
    <add key="TestTimeSpan" value="00:12:30"/>
    <add key="TestUri" value="http://fsharp.org" />
    <add key="TestGuid" value="{7B7EB384-FEBA-4409-B560-66FF63F1E8D0}"/>
  </appSettings>
  <connectionStrings>
    <add name="Test" connectionString="Server=.;Database=SomeDatabase;Integrated Security=true"/>
  </connectionStrings>
</configuration>

Reference the type provider assembly and configure it to use your app.settings file:

1: 
2: 
3: 
4: 
5: 
#r "FSharp.Configuration.dll"
#r "System.Configuration.dll"
open FSharp.Configuration

type Settings = AppSettings<"app.config">

Now you have typed access to your app.config files:

alt text

Reading and writing from the config

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
// read a value from the config
Settings.Test2
val it : string = "Some Test Value 5"

// verify the file name
Settings.ConfigFileName
val it : string = "C:\Code\FSharp.Configuration\docs\content\app.config"

// read a connection string from the config
Settings.ConnectionStrings.Test
val it : string = "Server=.;Database=SomeDatabase;Integrated Security=true"

Using AppSettingsProvider in *.fsx-script

The default executable is the current project .config. (Which is Fsi.exe.config in F# interactive.) How ever, if you want to modify the configuration of some other application, you can do with SelectExecutableFile-method:

1: 
2: 
3: 
let path = System.IO.Path.Combine [|__SOURCE_DIRECTORY__ ; "bin"; "myProject.exe" |]
Settings.SelectExecutableFile path
Settings.Test2
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Configuration
type Settings = AppSettings<...>

Full name: AppSettingsProvider.Settings
type AppSettings

Full name: FSharp.Configuration.AppSettings
property AppSettings<...>.Test2: string


Returns the value from app.config with key test2
property AppSettings<...>.ConfigFileName: string


Returns the Filename
type ConnectionStrings =
  static member LocalSqlServer : string with get, set
  static member Test : string with get, set

Full name: FSharp.Configuration.AppSettings,configFileName="app.config".ConnectionStrings


Represents the available connection strings from app.config
property AppSettings<...>.ConnectionStrings.Test: string


Returns the connection string from app.config with name Test
val path : string

Full name: AppSettingsProvider.path
namespace System
namespace System.IO
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
System.IO.Path.Combine([<System.ParamArray>] paths: string []) : string
System.IO.Path.Combine(path1: string, path2: string) : string
System.IO.Path.Combine(path1: string, path2: string, path3: string) : string
System.IO.Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
AppSettings<...>.SelectExecutableFile(pathOfExe: string) : Unit


Property to change the executable file that is read for configurations. This idea is that you can manage other executables also (e.g. from script).
Fork me on GitHub