Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code I normally use to automatically add applications to startup environment. It also includes a small piece of code that allows to bypass UAC protection.</p> <pre><code>using Microsoft.Win32; using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Security.Principal; using System.Windows.Forms; public static class Program { [STAThread] public static void Main() { if (!IsAdmin() &amp;&amp; IsWindowsVistaOrHigher()) RestartElevated(); else AddToStartup(true); } private static Boolean IsAdmin() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); if (identity != null) return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator); return false; } private static Boolean IsWindowsVistaOrHigher() { OperatingSystem os = Environment.OSVersion; return ((os.Platform == PlatformID.Win32NT) &amp;&amp; (os.Version.Major &gt;= 6)); } private static void AddToStartup(Boolean targetEveryone) { try { Environment.SpecialFolder folder = ((targetEveryone &amp;&amp; IsWindowsVistaOrHigher()) ? Environment.SpecialFolder.CommonStartup : Environment.SpecialFolder.Startup); String fileDestination = Path.Combine(Environment.GetFolderPath(folder), Path.GetFileName(Application.ExecutablePath)); if (!File.Exists(fileDestination)) File.Copy(Application.ExecutablePath, fileDestination); } catch { } try { using (RegistryKey main = (targetEveryone ? Registry.LocalMachine : Registry.CurrentUser)) { using (RegistryKey key = main.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { String fileName = Path.GetFileNameWithoutExtension(Application.ExecutablePath); if (key.GetValue(fileName) == null) key.SetValue(fileName, Application.ExecutablePath); } } } catch { } } private static void RestartElevated() { String[] argumentsArray = Environment.GetCommandLineArgs(); String argumentsLine = String.Empty; for (Int32 i = 1; i &lt; argumentsArray.Length; ++i) argumentsLine += "\"" + argumentsArray[i] + "\" "; ProcessStartInfo info = new ProcessStartInfo(); info.Arguments = argumentsLine.TrimEnd(); info.FileName = Application.ExecutablePath; info.UseShellExecute = true; info.Verb = "runas"; info.WorkingDirectory = Environment.CurrentDirectory; try { Process.Start(info); } catch { return; } Application.Exit(); } } </code></pre> <p>If you want to create just an application shortcut to the Startup folder instead of copying the whole file, take a look at <a href="http://www.vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/Creating_and_Modifying_Shortcuts/article.asp" rel="nofollow">this</a> and <a href="http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App" rel="nofollow">this</a> as it's not as simple as it might look at a first glance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload