C
C#8mo ago
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);
}
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);
}
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
6 Replies
Omnissiah
Omnissiah8mo ago
that worked up until some versions of windows ago, i don't think that's sufficient anymore
M B V R K
M B V R KOP8mo ago
Please is this any other efficient way ?
remi.nz
remi.nz8mo ago
One way could be creating a shortcut of your app and placing it in
%appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
%appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
string originalFilePath = "Your File Path";
string shortcutPath = "Path to appdata startup folder";

try
{
var wshShell = new IWshRuntimeLibrary.WshShell();

IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutPath);

shortcut.TargetPath = originalFilePath;
shortcut.Save();
}
catch (Exception)
{ }
string originalFilePath = "Your File Path";
string shortcutPath = "Path to appdata startup folder";

try
{
var wshShell = new IWshRuntimeLibrary.WshShell();

IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutPath);

shortcut.TargetPath = originalFilePath;
shortcut.Save();
}
catch (Exception)
{ }
M B V R K
M B V R KOP8mo ago
Where to find that IWshRuntimeLibrary please???
Omnissiah
Omnissiah8mo ago
try searching for add reference -> com -> windows scripting host
No description
remi.nz
remi.nz8mo ago
^^
Want results from more Discord servers?
Add your server