From caac16f88bcf4aae061e7c865b6e9011404525db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=BA=D0=BE=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Thu, 4 Jun 2026 19:46:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BD=D0=BE=D0=BF=D0=BA=D0=B0=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8?= =?UTF-8?q?=20=D1=81=D1=82=D0=BE=D0=BB=D0=B1=D0=B5=D1=86=20=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LinuxEnvironmentVariableStore.cs | 10 +++ .../WindowsEnvironmentVariableStore.cs | 45 +++++++++++-- .../IEnvironmentVariableStore.cs | 2 + .../Views/MainWindow.axaml | 34 +++++++--- .../Services/EnvironmentVariablesService.cs | 35 ++++++---- .../EnvironmentVariableRowViewModel.cs | 67 ++++++++++++------- .../MainWindowViewModel.cs | 17 +++++ 7 files changed, 155 insertions(+), 55 deletions(-) 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" /> - + + +