app.manifest
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
/// <summary> /// 在注冊表中添加、刪除開機自啟動鍵值 /// </summary> public static int SetAutoBootStatu(bool isAutoBoot) { try { string execPath = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); if (isAutoBoot) { rk2.SetValue("MyExec", execPath); Console.WriteLine(string.Format("[注冊表操作]添加注冊表鍵值:path = {0}, key = {1}, value = {2} 成功", rk2.Name, "TuniuAutoboot", execPath)); } else { rk2.DeleteValue("MyExec", false); Console.WriteLine(string.Format("[注冊表操作]刪除注冊表鍵值:path = {0}, key = {1} 成功", rk2.Name, "TuniuAutoboot")); } rk2.Close(); rk.Close(); return 0; } catch (Exception ex) { Console.WriteLine(string.Format("[注冊表操作]向注冊表寫開機啟動信息失敗, Exception: {0}", ex.Message)); return -1; } }