F# get environment variables as a generic collection

environment-variables, f#

Solution

I'm not sure whether .NET has any method that returns environment variables as a typed generic collection (the `GetEnvironmentVariables` method has been there since .NET 1.1 and so it is not generic). If you want to convert the result to a generic dictionary yourself, you can do something like this:

let envVars = 
  System.Environment.GetEnvironmentVariables()
  |> Seq.cast<System.Collections.DictionaryEntry>
  |> Seq.map (fun d -> d.Key :?> string, d.Value :?> string)
  |> dict

This first converts the result to a sequence of `DictionaryEntry` elements, then extracts key and value and casts them to a string and then builds `IDictionary<string, string>` using built-in `dict` function.

Problem

In .NET 4.5 Environment.GetEnvironmentVariables() returns the environment variables as a non-generic Collections.IDictionary. Is there any way to get the environment variables in F# as a generic collection?

Original source