Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# get environment variables as a generic collection

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?

like image 850
Marcus Junius Brutus Avatar asked Aug 17 '12 22:08

Marcus Junius Brutus


1 Answers

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.

like image 188
Tomas Petricek Avatar answered Sep 30 '22 09:09

Tomas Petricek