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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With