Search code examples
c#wpf

C# Application not run on startup, Startup impact "not measured" on Windows 10


I have a small WPF Application. I'm trying to get it started on Windows startup My C# code as follows:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    key.SetValue("MyApp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
                    key.Close();
                }

and deleted RegistryKey:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                key.DeleteValue("MyApp", false);
                key.Close();
            }

I've build one executable file (MyApp.exe) and installed on my computer. But the application still does not run when Windows start up. How can I do? how to change Startup impact "not measured" to other on Task Manager?

I'm using Windows 10 x64. Sorry for my English.

Thanks.


Solution

  • For people who have problems like me, I have solved my problem, it is that my application needs to run as administrator so if I code as above and set app.manifest

    <requestedExecutionLevel level = "requireAdministrator" UIAccess = "false "/>
    

    it will not run at startup Windows.

    To solve this I change my code from CurrentUser to LocalMachine:

     using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    key.SetValue("TechTemp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
                    key.Close();
                }
    

    and

     using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    key.DeleteValue("TechTemp", false);
                    key.Close();
                }
    

    The next problem was how to turn off UAC for your application (if UAC is enabled on your computer), you can see here. About Status impact, your application will still run at the window startup even the state is "not measured” on Task Manager. Thanks Rashid Malik and Sami

    Update

    You can read here to run application on windows startup without adding a registry key, this worked for me.