I have a WPF
application and I want to offer a checkbox that configures if the app will start on Windows startup (or not).
For now, I have it working with this:
void ManageRunOnStartup(bool runOnStartup)
{
var shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "myApp.lnk");
var shortcutExists = System.IO.File.Exists(shortcutPath);
if (runOnStartup)
{
if (shortcutExists == false)
CreateShortcut(shortcutPath);
}
else if (shortcutExists)
{
System.IO.File.Delete(shortcutPath);
}
}
void CreateShortcut(string shortcutPath)
{
var appLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var shell = new WshShell();
var shortcut = shell.CreateShortcut(shortcutPath) as IWshShortcut;
shortcut.TargetPath = appLocation;
shortcut.WorkingDirectory = Path.GetDirectoryName(appLocation);
shortcut.Save();
}
The thing is, to write/delete a file in the Environment.SpecialFolder.Startup
folder, the app requires Admin rights :(
Is there a way to achieve the same result but without requiring Admin rights?