0

I have Programatically created a shortcut of my tray application in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup. But my tray application is not launching when i login for any user. It is only starting for the administrator. Can someone please tell me, i have spend a complete day to solve this issue but unable to fix it..

    var startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
    var shell = new WshShell();
    var shortCutLinkFilePath = string.Format("{0}\\{1}", startupFolderPath, "MyShortcut.lnk");
    var windowsApplicationShortcut = (IWshShortcut)shell.CreateShortcut(shortCutLinkFilePath);
    windowsApplicationShortcut.Description = "Shortcut for My Tray application.";
    windowsApplicationShortcut.WorkingDirectory = assemblyPath;
    windowsApplicationShortcut.TargetPath = executablePath;
    windowsApplicationShortcut.Save();
meriaz
  • 65
  • 1
  • 11
  • While Installation check for all user option in the Installation wizard – madan Sep 10 '15 at 07:31
  • The InstallAllUsers property for a deployment project determines whether an application is installed for all users of a computer or only for the user performing the installation. The InstallAllUsers property can be set in the Properties window when a deployment project is selected in the Solution Explorer. **False** — The application will only be installed for the current user and will not be visible to other users of the computer (the default). **True** — The application will be installed for all users. –  Sep 10 '15 at 07:38
  • I am not getting it. Lets keep it simple for some time and forget about the installer. I am just programatically creating a shortcut of some other application. – meriaz Sep 10 '15 at 09:26
  • The application is configured to require admin privilege. Such applications can't be run from the Startup folder, except when logging in as the built-in Administrator account. Make sure the application has a manifest, and that it is configured for asInvoker rather than requiresAdmin. If you're using Visual Studio, those options are under Linker -> Manifest File. – Harry Johnston Sep 10 '15 at 23:25
  • What if my application needs to have the admin privilages? How can i invoke it from startup. – meriaz Sep 11 '15 at 06:34

1 Answers1

0

I think there are a couple of ways to deal with this:

  1. A program won't start with elevated permissions from the Startup folder (or similar "start at logon" locations), as Harry Johnston points out. But if it has an elevation manifest you should be able to relaunch it with a shell execute, then it will prompt for elevation because it's no longer directly from Startup. The Startup shortcut could have a command line option as a quick way to knows it's from Startup, then run the path to your executable with ShellExecute() or a Process.Start with a ProcessStartInfo.UseShellExecute set true. This should get the porompt for elevation which is required for elevated code to run. Caveat: I've not tried this but I've heard reports that it works.

  2. A service can run elevated and start when the system starts. Your startup program can be non-admin and have a message protocol to ask the service to do things. This is overkill for an app you just want to run for a short time, but not for a system monitor type of app.

PhilDW
  • 20,260
  • 1
  • 18
  • 28