using System; using System.Reflection; using System.Windows.Forms; using Microsoft.Win32; namespace Widow { public static class LaunchOnBoot { #region Public Methods /// /// Enable double buffering for an arbitrary control. /// /// the control to enable double buffering for /// true on success /// Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793 public static bool SetDoubleBuffered(this Control control) { if (SystemInformation.TerminalServerSession) { return false; } var dgvType = control.GetType(); var pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); if (pi == null) { return false; } pi.SetValue(control, true, null); return true; } public static void InvokeIfRequired(this T control, Action action) where T : Control { if (control.InvokeRequired) { control.BeginInvoke((MethodInvoker) delegate { action(control); }); return; } action(control); } public static bool Set(bool enable) { using (var key = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { if (key == null) { return false; } switch (enable) { case true: key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); break; default: key.DeleteValue(Constants.AssemblyName, false); break; } } return true; } public static bool Get() { using (var key = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { return key?.GetValue(Constants.AssemblyName) != null; } } #endregion } }