-
Notifications
You must be signed in to change notification settings - Fork 1
Add EnvironmentExtensions with RefreshEnvironmentVariables #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
92bb2a3
Add EnvironmentExtensions with RefreshEnvironmentVariables
Copilot 0fcf0bd
Explicitly specify EnvironmentVariableTarget.Process in SetEnvironmen…
Copilot c05e895
Remove LINQ, iterate IDictionary directly with typed DictionaryEntry …
Copilot ec1d74e
Guard RefreshEnvironmentVariables with Windows platform check, no-op …
Copilot afbcf1c
Switch platform guard to OperatingSystem.IsWindows()
Copilot c8894e4
Remove process variables that are no longer in machine-level environment
Copilot cc5de5b
Update EnvironmentExtensions.cs
Tyrrrz ed2cb11
Update PowerKit/Extensions/EnvironmentExtensions.cs
Tyrrrz 125c131
Include user-level variables in RefreshEnvironmentVariables
Copilot 3ec55dd
Update EnvironmentExtensions.cs
Tyrrrz 2604c76
Update PowerKit/Extensions/EnvironmentExtensions.cs
Tyrrrz d9b7d13
Update EnvironmentExtensions.cs
Tyrrrz a6f4744
Update EnvironmentExtensions.cs
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| { | ||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var machineVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine); | ||
| var userVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User); | ||
|
|
||
|
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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.