Skip to content
Merged
Changes from 5 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
31 changes: 31 additions & 0 deletions PowerKit/Extensions/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 environment variables.
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
/// Only has an effect on Windows; on other platforms, this method is a no-op.
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
/// </summary>
public static void RefreshEnvironmentVariables()
{
Comment thread
Tyrrrz marked this conversation as resolved.
if (!OperatingSystem.IsWindows())
{
return;
}

foreach (DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine))
{
var key = (string)environmentVariable.Key;
var value = (string?)environmentVariable.Value;

Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.Process);
}
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}
}
}