0

This is my code, i need my service to make a shortcut to an application on user desktop if the shortcut not exist. when i debug my application inside visual studio it will put the shortcut on my desktop, but when i run the service on windows it always will put the shortcut on C Drive not on my desktop.

  private void CreateShortcut()
    {
        object shDesktop = (object)"Desktop";
        WshShell shell = new WshShell();
        //string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\CadEisancy.lnk";
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
        shortcut.Description = "New shortcut for a Notepad";
        shortcut.Hotkey = "Ctrl+Shift+N";
        shortcut.TargetPath = @"C:\RankWindowsApp\RankWindowsApp\bin\Debug\RankWindowsApp.exe";
        shortcut.IconLocation = @"C:\RankWindowsApp\Icon.ico";
        shortcut.Save();
    }
    protected override void OnStart(string[] args)
    {
        //Create Shortcut On Desktop
        CreateShortcut();
    }

My code:

![MyCode][1]

  • Hi! Please post your code in a `code` block and not as image. – div Aug 31 '19 at 19:55
  • 1
    Duplicate to https://stackoverflow.com/questions/22232646/enumerate-windows-of-user-desktop-on-another-session – Lex Li Sep 01 '19 at 00:51
  • @LexLi Its not duplicated. – Tawfeeq Amarneh Sep 01 '19 at 02:50
  • 1
    If you dig further enough, you should see why this one is duplicate. Windows session isolation is a complex concept that most developers don't realize, but everyone who writes Windows service must study it, or questions like yours won't be answered/resolved. The linked thread already covers the necessary background information. – Lex Li Sep 01 '19 at 04:12

2 Answers2

0

The solution is to get the current logged user session code, then get the name of his Profile depending on the session and finally we can specify his desktop path.

0

How your service is configured ? If you are not running under user account, the service will use its own profile as you see in your output .

I have answered mostly similar answer in the following look. Please have a look if you feel that helps.

https://stackoverflow.com/a/46540108/1556780

Praveen M
  • 443
  • 2
  • 11
  • Thank you.The solution is to get the current logged user session code, then get the name of his Profile depending on the session and finally we can specify his desktop path. – Tawfeeq Amarneh Sep 05 '19 at 08:37