0

My Application will not launch after Windows startup, the application is registered in the register editor with the right path to my application exe.

Register Editor ScreenShot

here is my code to register my application to the register editor

       private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RUN", true);


        if (e.PropertyName == nameof(isStartOnStartup))
        {
            if (isStartOnStartup)
            {
                reg.SetValue("CommunicationHub", Process.GetCurrentProcess().MainModule.FileName);
                Settings.Default.Startup = true;
                Settings.Default.Save();

            }
            else
            {
                reg.DeleteValue("CommunicationHub");
                Settings.Default.Startup = false;
                Settings.Default.Save();
            }
        }


    }

I have checked EventViewer for crashes and have aswell added logging to the application root, both gave me nothing.

I have disabled UAC, to check if it was the administrator right that was hindering the application from running, still no result.

i feel lost, i hope someone can help me with the problem..

sidadus
  • 1
  • 1
  • https://stackoverflow.com/questions/21348289/launch-wpf-application-on-windows-startup – demonplus Mar 31 '22 at 13:16
  • @demonplus the solution in the thread didnt work – sidadus Mar 31 '22 at 14:06
  • If you're seeing your entry in the Registry Editor, then you know your code is working as expected to add it to the Registry. Some things I would try to debug. 1) See if you are hitting some undocumented path length limit by moving your CommunicationHub.exe to another path, say c:\users\ska\Desktop\CommunicationHub.exe 2) See if you can reproduce the problem with a console app that launches and stays open (e.g. Console.ReadLine()) 3) Try registering to HKLM instead of HKCU and see if that one works. – Tam Bui Mar 31 '22 at 16:02
  • I have encountered this problem, try setting the path of the program to another driver path – sync Apr 01 '22 at 01:37
  • It´s working without admin rights, i´m confuced why didnt it work before when the slider in the UAC was down? `From to this ` my next question is how do i make windows accept my program to run after startup with admin rights? – sidadus Apr 01 '22 at 06:19

1 Answers1

0

Found another way to do this, instead of having my application in the register editor, but in Task Schedular, it will launch the application on windows startup with the admin rights. BUT! Only if I tell task scheduler to launch it as "at log on" trigger and not on "at system startup". But if I combine td.Triggers.AddNew(TaskTriggerType.Boot); and td.Principal.UserId = "SYSTEM"; it will give me the status running but the UI is not showing. Why?.. How do I fix this?

Here is my updated code, and I found a library to do the schedular thing.

library: https://github.com/dahall/TaskScheduler

{
    TaskService ts = new TaskService();
    string program_path = Process.GetCurrentProcess().MainModule.FileName; 

    if (e.PropertyName == nameof(isStartOnStartup))
    {
        if (isStartOnStartup)
        {
            TaskDefinition td = ts.NewTask();

            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Triggers.AddNew(TaskTriggerType.Boot); // Not working  
            td.Principal.UserId = "SYSTEM"; // Run whether user is logged on or not. combined with line 132 runs the program but the ui is invicible
            //td.Triggers.AddNew(TaskTriggerType.Logon); // Runs and shows the UI perfectly fine
            td.Actions.Add(new ExecAction(program_path, null));
            ts.RootFolder.RegisterTaskDefinition("CommunicationHub", td);

            Settings.Default.Startup = true;
            Settings.Default.Save();
        }
        else
        {
            ts.RootFolder.DeleteTask("CommunicationHub", false);
            Settings.Default.Startup = false;
            Settings.Default.Save();
        }
    }
} 
Kent Kostelac
  • 2,257
  • 3
  • 29
  • 42
sidadus
  • 1
  • 1