C
C#8mo ago
M B V R K

Why the shortcut of my App in the Startup Folder not starts??

Hi guys, hope you're doing well, I have a Console App, in the program.cs I have the following code:
var worker = new Worker();

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

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

// Path to the Startup folder
string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

// Path to your application executable
string appExecutablePath = Process.GetCurrentProcess().MainModule.FileName;

// Create a WshShell object
var wshShell = new WshShell();

// Create a shortcut object
IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(
Path.Combine(startupFolderPath, "MBVRK.OfficeDRPC.lnk"));

// Set the target path of the shortcut
shortcut.TargetPath = appExecutablePath;

// Save the shortcut
shortcut.Save();

worker.Start();

// Keep the app running
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{
var path = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "log.txt");

// Open or create the log file and append text to it
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + ex.Message);
}
}
finally
{
Environment.Exit(0);
}
var worker = new Worker();

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

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

// Path to the Startup folder
string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

// Path to your application executable
string appExecutablePath = Process.GetCurrentProcess().MainModule.FileName;

// Create a WshShell object
var wshShell = new WshShell();

// Create a shortcut object
IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(
Path.Combine(startupFolderPath, "MBVRK.OfficeDRPC.lnk"));

// Set the target path of the shortcut
shortcut.TargetPath = appExecutablePath;

// Save the shortcut
shortcut.Save();

worker.Start();

// Keep the app running
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{
var path = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "log.txt");

// Open or create the log file and append text to it
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + ex.Message);
}
}
finally
{
Environment.Exit(0);
}
Question: Please, why my App don't start when windows starts ??? Massive thanks in advance <3
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server