diff --git a/src/Sms.Environment.Linux/LinuxEnvironmentVariableStore.cs b/src/Sms.Environment.Linux/LinuxEnvironmentVariableStore.cs index 7ce3a1c..bf32c24 100644 --- a/src/Sms.Environment.Linux/LinuxEnvironmentVariableStore.cs +++ b/src/Sms.Environment.Linux/LinuxEnvironmentVariableStore.cs @@ -36,6 +36,16 @@ public sealed class LinuxEnvironmentVariableStore : IEnvironmentVariableStore return System.Environment.GetEnvironmentVariable(name); } + public string? GetUserPersistedValue(string name) + { + if (LoadMergedFromDirectory().TryGetValue(name, out var value)) + { + return value; + } + + return null; + } + public void Set(string name, string value) { try diff --git a/src/Sms.Environment.Windows/WindowsEnvironmentVariableStore.cs b/src/Sms.Environment.Windows/WindowsEnvironmentVariableStore.cs index 4f2f35f..12cfdb0 100644 --- a/src/Sms.Environment.Windows/WindowsEnvironmentVariableStore.cs +++ b/src/Sms.Environment.Windows/WindowsEnvironmentVariableStore.cs @@ -1,4 +1,5 @@ using System.Runtime.InteropServices; +using Microsoft.Win32; using Sms.Environment; namespace Sms.Environment.Windows; @@ -7,14 +8,23 @@ public sealed class WindowsEnvironmentVariableStore : IEnvironmentVariableStore { private const int HWND_BROADCAST = 0xffff; private const int WM_SETTINGCHANGE = 0x001A; + private const string EnvironmentKeyPath = "Environment"; public string? Get(string name) => - System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User) + GetUserPersistedValue(name) ?? System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); + public string? GetUserPersistedValue(string name) + { + using var key = Registry.CurrentUser.OpenSubKey(EnvironmentKeyPath); + return key?.GetValue(name) as string; + } + public void Set(string name, string value) { - System.Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.User); + using var key = Registry.CurrentUser.OpenSubKey(EnvironmentKeyPath, writable: true) + ?? Registry.CurrentUser.CreateSubKey(EnvironmentKeyPath, writable: true); + key.SetValue(name, value, RegistryValueKind.ExpandString); System.Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Process); BroadcastEnvironmentChange(); } @@ -39,15 +49,36 @@ public sealed class WindowsEnvironmentVariableStore : IEnvironmentVariableStore public IReadOnlyDictionary GetProcessEnvironment() => ToDictionary(System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)); - public IReadOnlyDictionary GetUserPersistedEnvironment() => - ToDictionary(System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)); + public IReadOnlyDictionary GetUserPersistedEnvironment() + { + var result = new Dictionary(StringComparer.Ordinal); + using var key = Registry.CurrentUser.OpenSubKey(EnvironmentKeyPath); + if (key is null) + { + return result; + } - public bool IsPersistedInUserStore(string name) => - System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User) is not null; + foreach (var name in key.GetValueNames()) + { + if (key.GetValue(name) is string value) + { + result[name] = value; + } + } + + return result; + } + + public bool IsPersistedInUserStore(string name) + { + using var key = Registry.CurrentUser.OpenSubKey(EnvironmentKeyPath); + return key?.GetValue(name) is not null; + } public void RemoveFromUserStore(string name) { - System.Environment.SetEnvironmentVariable(name, null, EnvironmentVariableTarget.User); + using var key = Registry.CurrentUser.OpenSubKey(EnvironmentKeyPath, writable: true); + key?.DeleteValue(name, throwOnMissingValue: false); System.Environment.SetEnvironmentVariable(name, null, EnvironmentVariableTarget.Process); BroadcastEnvironmentChange(); } diff --git a/src/Sms.Environment/IEnvironmentVariableStore.cs b/src/Sms.Environment/IEnvironmentVariableStore.cs index e961852..8a9f250 100644 --- a/src/Sms.Environment/IEnvironmentVariableStore.cs +++ b/src/Sms.Environment/IEnvironmentVariableStore.cs @@ -4,6 +4,8 @@ public interface IEnvironmentVariableStore { string? Get(string name); + string? GetUserPersistedValue(string name); + void Set(string name, string value); bool Exists(string name); diff --git a/src/Sms.TaskTwo.Avalonia/Views/MainWindow.axaml b/src/Sms.TaskTwo.Avalonia/Views/MainWindow.axaml index 6d20673..9b693ef 100644 --- a/src/Sms.TaskTwo.Avalonia/Views/MainWindow.axaml +++ b/src/Sms.TaskTwo.Avalonia/Views/MainWindow.axaml @@ -41,10 +41,18 @@ Click="OnCloseClick" /> - + + +