Skip to content
Merged
Changes from all 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
62 changes: 62 additions & 0 deletions PowerKit/Extensions/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections;

namespace PowerKit.Extensions;

internal static class EnvironmentExtensions
{
extension(Environment)
{
/// <summary>
/// Refreshes the environment variables of the current process by re-applying
/// the machine-level and user-level environment variables.
/// </summary>
/// <remarks>
/// On Windows, this first removes any process-level environment variables that
/// are not present at the machine or user scope, then applies the machine-level
/// and user-level values to the current process.
/// This can remove variables inherited from the parent process or set by the
/// application itself.
/// On other platforms, this method is a no-op.
/// </remarks>
public static void RefreshEnvironmentVariables()
{
Comment thread
Tyrrrz marked this conversation as resolved.
if (!OperatingSystem.IsWindows())
{
return;
}

var machineVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);
var userVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);

Comment thread
Tyrrrz marked this conversation as resolved.
// Remove missing
foreach (DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process))
{
var key = (string)environmentVariable.Key;

if (!machineVariables.Contains(key) && !userVariables.Contains(key))
{
Environment.SetEnvironmentVariable(key, null, EnvironmentVariableTarget.Process);
}
}

// Add/set machine variables
foreach (DictionaryEntry environmentVariable in machineVariables)
{
var key = (string)environmentVariable.Key;
var value = (string?)environmentVariable.Value;

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

// Add/set user variables
foreach (DictionaryEntry environmentVariable in userVariables)
{
var key = (string)environmentVariable.Key;
var value = (string?)environmentVariable.Value;

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