sparse-intern-71089
04/01/2021, 4:22 PMbored-oyster-3147
04/01/2021, 4:25 PMpair.Key
is string
at compile-time so env[pair.key] = ...;
might give you an error?bored-oyster-3147
04/01/2021, 4:30 PMbored-oyster-3147
04/01/2021, 4:30 PMbored-oyster-3147
04/01/2021, 4:34 PMtall-needle-56640
04/01/2021, 4:44 PMobject
and not string
.bored-oyster-3147
04/01/2021, 5:17 PMelement
is of type System.Collection.DictionaryEntry?
.
And none of them satisfied KeyValuePair<string, object>
, KeyValuePair<string, string>
, or KeyValuePair<string, string?>
... which is concerning
I actually had to do this to get working:
var env = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry? element in env)
{
if (element is null || !element.HasValue)
continue;
if (element.Value.Key is string keyStr
&& element.Value.Value != null
&& element.Value.Value is string valueStr)
Console.WriteLine($"[{keyStr}] = {valueStr}");
}
bored-oyster-3147
04/01/2021, 5:19 PMis KeyValuePair<string, object>
check might never be satisfied currentlybored-oyster-3147
04/01/2021, 5:22 PMprehistoric-coat-10166
04/01/2021, 5:25 PMSystem.GetEnvironmentVariables
prehistoric-coat-10166
04/01/2021, 5:25 PMforeach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
should do the trick and then checking the key & valuebored-oyster-3147
04/01/2021, 5:26 PMbored-oyster-3147
04/01/2021, 5:29 PMtall-needle-56640
04/01/2021, 5:29 PMelement.Value.Value != null
&& element.Value.Value is string valueStr
tall-needle-56640
04/01/2021, 5:30 PMbored-oyster-3147
04/01/2021, 5:31 PMprehistoric-coat-10166
04/01/2021, 5:33 PMif (element.Value.Key is string keyString
&& element.Value.Value is string valueString)
{
env[keyString] = valueString;
}
Should work (with the element null check)enough-garden-22763
04/01/2021, 5:38 PMenough-garden-22763
04/01/2021, 5:39 PMbored-oyster-3147
04/01/2021, 5:45 PMprehistoric-coat-10166
04/01/2021, 5:46 PMprehistoric-coat-10166
04/01/2021, 5:52 PMprehistoric-coat-10166
04/01/2021, 5:53 PMValueWhichMayBeNull ?? ""
prehistoric-coat-10166
04/01/2021, 6:01 PMProcess
https://github.com/dotnet/runtime/issues/34446