How to start a C # application when Windows starts?

I made an application that starts at startup, with the following code below.
The process starts on the process manager tool after restarting, but I do not see the application on the screen. When I open the same .exe file from the startup registry value, the program works fine.

// The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); // Add the value in the registry so that the application runs at startup rkApp.SetValue("MyApp", Application.ExecutablePath.ToString()); 

What can I do to fix this?

+59
c # startup registry
Feb 23 '11 at 10:25
source share
12 answers

Code here (application for the win form):

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; namespace RunAtStartup { public partial class frmStartup : Form { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); public frmStartup() { InitializeComponent(); // Check to see the current state (running at startup or not) if (rkApp.GetValue("MyApp") == null) { // The value doesn't exist, the application is not set to run at startup chkRun.Checked = false; } else { // The value exists, the application is set to run at startup chkRun.Checked = true; } } private void btnOk_Click(object sender, EventArgs e) { if (chkRun.Checked) { // Add the value in the registry so that the application runs at startup rkApp.SetValue("MyApp", Application.ExecutablePath); } else { // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("MyApp", false); } } } } 
+55
Mar 06 2018-11-11T00:
source share

Try this code

 private void RegisterInStartup(bool isChecked) { RegistryKey registryKey = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (isChecked) { registryKey.SetValue("ApplicationName", Application.ExecutablePath); } else { registryKey.DeleteValue("ApplicationName"); } } 

Source: http://www.dotnetthoughts.net/2010/09/26/run-the-application-at-windows-startup/

+35
Apr 03 2018-11-11T00:
source share

You can try copying the shortcut to the application into the startup folder instead of adding it to the registry. You can get the path using Environment.SpecialFolder.Startup . This has been available on all .net infrastructures since version 1.1.

Alternatively, you might find this site useful, it lists many different ways you can get the application to start automatically.

+17
Feb 23 '11 at 10:43
source share
 public class StartUpManager { public static void AddApplicationToCurrentUserStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""); } } public static void AddApplicationToAllUserStartup() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""); } } public static void RemoveApplicationFromCurrentUserStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My ApplicationStartUpDemo", false); } } public static void RemoveApplicationFromAllUserStartup() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My ApplicationStartUpDemo", false); } } public static bool IsUserAdministrator() { //bool value to hold our return value bool isAdmin; try { //get the currently logged in user WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); } catch (UnauthorizedAccessException ex) { isAdmin = false; } catch (Exception ex) { isAdmin = false; } return isAdmin; } } 

you can check out the whole article here

+9
Mar 02 '15 at 18:01
source share

1- add namespace :

 using Microsoft.Win32; 

2- add an application for registration:

 RegistryKey key=Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); key.SetValue("your_app_name", Application.ExecutablePath); 

if you want to remove the application from the registry :

 key.DeleteValue("your_app_name",false); 
+2
Nov 26 '18 at 6:35
source share

I did not find any of the above code. Maybe because .NET 3.5 works in my application. I dont know. The following code worked fine for me. I got this from a senior level .NET developer on my team.

 Write (Microsoft.Win32.Registry.LocalMachine, @ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \", "WordWatcher", "\" "+ Application.ExecutablePath.ToString () +" \ "");
 public bool Write(RegistryKey baseKey, string keyPath, string KeyName, object Value) { try { // Setting RegistryKey rk = baseKey; // I have to use CreateSubKey // (create or open it if already exits), // 'cause OpenSubKey open a subKey as read-only RegistryKey sk1 = rk.CreateSubKey(keyPath); // Save the value sk1.SetValue(KeyName.ToUpper(), Value); return true; } catch (Exception e) { // an error! MessageBox.Show(e.Message, "Writing registry " + KeyName.ToUpper()); return false; } } 
+1
Apr 16 '13 at 19:28
source share

I tried the code below first and it didn't work

 RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString()); 

Then I changed CurrentUser to LocalMachine and it works

 RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString()); 
+1
May 29 '13 at
source share

An open source application called "Startup Creator" customizes Windows startup by creating a script, providing an easy to use interface. Using powerful VBScript, it allows you to run applications or services with time intervals of delay, always in the same order. These scripts are automatically placed in your startup folder and can be opened back to make changes in the future.

http://startupcreator.codeplex.com/

0
Nov 22 '14 at 20:43
source share

for WPF: (where lblInfo is the label, chkRun is the checkBox)

this.Topmost is just to save my application on top of other windows, you will also need to add a using statement using Microsoft.Win32; ", StartupWithWindows is my application name

 public partial class MainWindow : Window { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); public MainWindow() { InitializeComponent(); if (this.IsFocused) { this.Topmost = true; } else { this.Topmost = false; } // Check to see the current state (running at startup or not) if (rkApp.GetValue("StartupWithWindows") == null) { // The value doesn't exist, the application is not set to run at startup, Check box chkRun.IsChecked = false; lblInfo.Content = "The application doesn't run at startup"; } else { // The value exists, the application is set to run at startup chkRun.IsChecked = true; lblInfo.Content = "The application runs at startup"; } //Run at startup //rkApp.SetValue("StartupWithWindows",System.Reflection.Assembly.GetExecutingAssembly().Location); // Remove the value from the registry so that the application doesn't start //rkApp.DeleteValue("StartupWithWindows", false); } private void btnConfirm_Click(object sender, RoutedEventArgs e) { if ((bool)chkRun.IsChecked) { // Add the value in the registry so that the application runs at startup rkApp.SetValue("StartupWithWindows", System.Reflection.Assembly.GetExecutingAssembly().Location); lblInfo.Content = "The application will run at startup"; } else { // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("StartupWithWindows", false); lblInfo.Content = "The application will not run at startup"; } } } 
0
Aug 31 '15 at 13:19
source share

If you cannot configure the application to start automatically, you can try inserting this code into the manifest

 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> 

or delete the manifest, I found it in my application

0
Dec 26 '16 at 10:09
source share

I think there is a certain call to the Win32 API that takes the path to the program and automatically puts it in the registry for you in the right place, I used it in the past, but I no longer remember the name of the function.

-one
Feb 23 '11 at 10:35
source share

OK, here are my 2 cents: try paving the path with each backslash as a double backslash. I found that sometimes calling the WIN API requires this.

-one
Feb 23 2018-11-11T00:
source share



All Articles