C#C
C#2y ago
7 replies
M B V R K

Why Console App don't Auto Run at Windows Startup ?

Hi guys, hope you're doing well,
My question is very simple, I have a
Console App
, in the
program.cs
, in the
Main
method I have wrote this code :
            var worker = new Worker();

            try
            {
                // Hide the console window
                FreeConsole();

                var currentProcess = Process.GetCurrentProcess();
                currentProcess.PriorityClass = ProcessPriorityClass.BelowNormal;

                // Register the app to be auto startup
                using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    key?.SetValue("MBVRK.OfficeDRPC", System.Reflection.Assembly.GetExecutingAssembly().Location);
                }

                worker.Start();

                // Keep the app running
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception)
            {
                Environment.Exit(0);
            }

            Environment.Exit(0);
        }


The issue is in the part where I add the application to the auto run programs when
Windows
startup using
Registry

                using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    key?.SetValue("MBVRK.OfficeDRPC", System.Reflection.Assembly.GetExecutingAssembly().Location);
                }


But it doesn't work and the app don't run when windows statup.

I go to this
Registry
path
Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
and I found that the app registred itself as the code written but
windows
don't auto start it up.

Please any fix for this issue?

Massive thanks in advance <3
Was this page helpful?