private async void OnEditKey() { // Init modification status ModificationMode = ModificationModeType.EditKey; ModificationTitle = (_currentAppMode == AppMode.User) ? Constants.MODE_EDIT_USER_KEY : Constants.MODE_EDIT_SYSTEM_KEY; ModifiedKey = ActiveKey; ModifiedValue = _currentVars[ActiveKey].Data; ValidationMessage = string.Empty; // Show modification dialog var result = await _modificationService.InitModification(this); if (result != true) return; // NOTE: Each Environment Variable should have a unique key and // since the Key of the Environment variable is being edited, // it should result in the creation of a new EnVar object and // the deletion of the old EnVar object IsProgressVisible = true; // Create the new Environment variable var newVar = new EnVar(_modifiedKey, _modifiedValue); var keyList = new List<string>(); var delVar = _currentVars[ActiveKey]; _currentVars.Remove(ActiveKey); _currentVars[newVar.Key] = newVar; keyList.AddRange(_currentVars.Keys); // Sort the keys keyList.Sort(); DisplayKeys = new ObservableCollection<string>(keyList); ActiveKey = _modifiedKey; await Task.Run(new Action(() => { // Remove the old Environment variable RegistryManager.DeleteEnvironmentVariable(delVar, _target); // Commit the new Environment variable RegistryManager.SaveEnvironmentVariable(newVar, _target); })); IsProgressVisible = false; }
private async void OnNewKey() { // Init modification status ModificationMode = ModificationModeType.NewKey; ModificationTitle = (_currentAppMode == AppMode.User) ? Constants.MODE_NEW_USER_KEY : Constants.MODE_NEW_SYSTEM_KEY; ModifiedKey = string.Empty; ModifiedValue = string.Empty; ValidationMessage = string.Empty; // Show modification dialog var result = await _modificationService.InitModification(this); if (result != true) return; IsProgressVisible = true; // Create the new Environment variable var newVar = new EnVar(_modifiedKey, _modifiedValue); _currentVars[newVar.Key] = newVar; var keyList = new List<string>(_currentVars.Keys); // Sort the keys keyList.Sort(); DisplayKeys = new ObservableCollection<string>(keyList); ActiveKey = _modifiedKey; // Commit the new Environment variable await Task.Run(new Action(() => { RegistryManager.SaveEnvironmentVariable(newVar, _target); })); IsProgressVisible = false; }
public static void DeleteEnvironmentVariable(EnVar enVar, EnvironmentVariableTarget varType) { Environment.SetEnvironmentVariable(enVar.Key, null, varType); }
public void InitializeEnvironmentVariables() { InitResult = InitResultType.None; // User Environment Variables try { _userVars.Clear(); // Get the User Environment Variables var theKey = Registry.CurrentUser; theKey = theKey.OpenSubKey(Constants.USER_ENV_SUBKEY, true); // GetValueNames will return the names of all the keys within the Enviroment Key foreach (var key in theKey.GetValueNames()) { var keyValue = (theKey.GetValue(key)).ToString(); _userVars[key] = new EnVar(key, keyValue); } theKey.Close(); } catch (SecurityException) { // MessageBox.Show(e.Message + " Please restart application in Administrator privileges.", "NVM"); InitResult = InitResultType.AccessDenied; } catch (Exception) { InitResult = InitResultType.OtherError; } // System Environment Variables try { _systemVars.Clear(); // Get the System Environment Variables var theKey = Registry.LocalMachine; theKey = theKey.OpenSubKey(Constants.SYSTEM_ENV_SUBKEY, true); // GetValueNames will return the names of all the keys within the Enviroment Key foreach (var key in theKey.GetValueNames()) { var keyValue = (theKey.GetValue(key)).ToString(); _systemVars[key] = new EnVar(key, keyValue); } theKey.Close(); } catch (SecurityException) { // Unable to access Environment Variables InitResult = InitResultType.AccessDenied; } catch (Exception e) { // Some other unexpected error has occured. Log it! EventLog.WriteEntry("NVM#", "Initialization Error: " + Environment.NewLine + e, EventLogEntryType.Error); InitResult = InitResultType.OtherError; } if (InitResult == InitResultType.None) { // Environment Variables accessed successfully InitResult = InitResultType.InitOk; } }
public static void SaveEnvironmentVariable(EnVar enVar, EnvironmentVariableTarget varType) { Environment.SetEnvironmentVariable(enVar.Key, enVar.Data, varType); }