Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions PowerKit/Extensions/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections;
using System.Linq;

namespace PowerKit.Extensions;

internal static class EnvironmentExtensions
{
extension(Environment)
{
/// <summary>
/// Refreshes the environment variables of the current process by re-applying
/// the machine-level environment variables.
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
/// </summary>
public static void RefreshEnvironmentVariables()
{
Comment thread
Tyrrrz marked this conversation as resolved.
var machineEnvironmentVariables = Environment
.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)
.Cast<DictionaryEntry>();

Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
foreach (var environmentVariable in machineEnvironmentVariables)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
var key = (string)environmentVariable.Key;
var value = (string?)environmentVariable.Value;

Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.Process);
}
}
}
}