public T Load <T>(string valueName) { try { if (registryKeyReadOnly == null) { registryKeyReadOnly = Registry.CurrentUser.OpenSubKey(registryPath); } if (registryKeyReadOnly != null) { object value = registryKeyReadOnly.GetValue(valueName, null); SettingsLoaded?.Invoke(this, registryKeyReadOnly); if (typeof(T) == typeof(bool)) { return((T)Convert.ChangeType((int)value > 0, typeof(T))); } else { return((T)value); } } return(default(T)); } catch (SecurityException exception) { Debug.WriteLine(exception); RegistryAccessFailed?.Invoke(this, registryKeyReadOnly); return(default(T)); } catch (Exception exception) { Debug.WriteLine(exception); return(default(T)); } }
public void Save() { if (savingOptions == PersistWindowStateSavingOptions.Registry) { RegistryKey registryKey = null; try { registryKey = Registry.CurrentUser.CreateSubKey(registryPath); } catch (SecurityException exception) { Debug.WriteLine(exception); RegistryAccessFailed?.Invoke(this, registryKey, exception); } catch (Exception exception) { Debug.WriteLine(exception); } if (registryKey == null) { return; } if (allowSaveTopMost) { registryKey.SetValue(parent.Name + "TopMost", parent.TopMost ? 1 : 0); } if (!disableSavePosition && moved) { registryKey.SetValue(parent.Name + "Left", normalLeft); registryKey.SetValue(parent.Name + "Top", normalTop); } if (!disableSaveSize && (parent.FormBorderStyle == FormBorderStyle.Sizable || parent.FormBorderStyle == FormBorderStyle.SizableToolWindow)) { if (!disableSaveWidth && normalWidth > 0) { registryKey.SetValue(parent.Name + "Width", normalWidth); } if (!disableSaveHeight && normalHeight > 0) { registryKey.SetValue(parent.Name + "Height", normalHeight); } } if (!disableSaveWindowState && parent.ControlBox && (parent.MinimizeBox || parent.MaximizeBox)) { if (allowSaveMinimized || parent.WindowState != FormWindowState.Minimized) { registryKey.SetValue(parent.Name + "WindowState", (int)parent.WindowState); } else { registryKey.SetValue(parent.Name + "WindowState", (int)FormWindowState.Normal); } } WindowStateSaved?.Invoke(this, registryKey); } }
public void Save(string valueName, object value) { try { if (registryKeyWritable == null) { registryKeyWritable = Registry.CurrentUser.CreateSubKey(registryPath); } registryKeyWritable.SetValue(valueName, value == null ? string.Empty : value.GetType() == typeof(bool) ? (bool)value ? 1 : 0 : value); SettingsSaved?.Invoke(this, registryKeyWritable); } catch (SecurityException exception) { Debug.WriteLine(exception); RegistryAccessFailed?.Invoke(this, registryKeyWritable); } catch (Exception exception) { Debug.WriteLine(exception); } }
private void OnLoad(object sender, EventArgs e) { if (savingOptions == PersistWindowStateSavingOptions.None || string.IsNullOrEmpty(registryPath)) { if (firstLoad) { OnResize(sender, e); OnMove(sender, e); if (placeOnScreenAfterLoad) { parent.Location = AdjustLocation(new Point(normalLeft, normalTop), parent.Size); } firstLoad = false; } else { if (allowSaveTopMost) { parent.TopMost = topMost; } if (!disableSaveSize && (parent.FormBorderStyle == FormBorderStyle.Sizable || parent.FormBorderStyle == FormBorderStyle.SizableToolWindow)) { if (!disableSaveWidth) { parent.Width = normalWidth; } if (!disableSaveHeight) { parent.Height = normalHeight; } } if (placeOnScreenAfterLoad && !disableSavePosition) { parent.Location = AdjustLocation(new Point(normalLeft, normalTop), parent.Size); } else if (!disableSavePosition) { parent.Location = new Point(normalLeft, normalTop); } if (!disableSaveWindowState && parent.ControlBox && (parent.MinimizeBox || parent.MaximizeBox)) { parent.WindowState = allowSaveMinimized || parent.WindowState != FormWindowState.Minimized ? windowState : FormWindowState.Normal; } } } else if (savingOptions == PersistWindowStateSavingOptions.Registry) { RegistryKey registryKey = null; try { registryKey = Registry.CurrentUser.OpenSubKey(registryPath); } catch (SecurityException exception) { Debug.WriteLine(exception); RegistryAccessFailed?.Invoke(this, registryKey, exception); } catch (Exception exception) { Debug.WriteLine(exception); } if (registryKey != null && !IsAlreadyRunning()) { if (allowSaveTopMost) { topMost = parent.TopMost = (int)registryKey.GetValue(parent.Name + "TopMost", parent.TopMost) > 0; } normalLeft = (int)registryKey.GetValue(parent.Name + "Left", parent.Location.X); normalTop = (int)registryKey.GetValue(parent.Name + "Top", parent.Location.Y); if (!disableSaveSize && (parent.FormBorderStyle == FormBorderStyle.Sizable || parent.FormBorderStyle == FormBorderStyle.SizableToolWindow)) { if (!disableSaveWidth) { int width = (int)registryKey.GetValue(parent.Name + "Width", parent.Width); if (width > 0) { parent.Width = width; firstLoad = false; } } if (!disableSaveHeight) { int height = (int)registryKey.GetValue(parent.Name + "Height", parent.Height); if (height > 0) { parent.Height = height; firstLoad = false; } } } if (placeOnScreenAfterLoad && !disableSavePosition) { parent.Location = AdjustLocation(new Point(normalLeft, normalTop), parent.Size); } else if (!disableSavePosition) { parent.Location = new Point(normalLeft, normalTop); } normalWidth = parent.Width; normalHeight = parent.Height; if (!disableSaveWindowState && parent.ControlBox && (parent.MinimizeBox || parent.MaximizeBox)) { windowState = (FormWindowState)registryKey.GetValue(parent.Name + "WindowState", (int)parent.WindowState); if (!allowSaveMinimized && windowState == FormWindowState.Minimized) { windowState = FormWindowState.Normal; } parent.WindowState = windowState; } WindowStateLoaded?.Invoke(this, registryKey); } } parent.Resize += new EventHandler(OnResize); parent.Move += new EventHandler(OnMove); }