0

This question explains how to create a shortcut in C#. For example,

using System;
using IWshRuntimeLibrary;
using System.IO;

namespace TestCreateShortcut
{
    class Program
    {
        static void Main(string[] args)
        {
            WshShell shell = new WshShell();
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string source = "C:\\foo\\hello.exe";
            string dest = desktop + "\\hello.lnk";
            IWshRuntimeLibrary.IWshShortcut shortcut =
                (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(dest);

            shortcut.TargetPath = source;
            shortcut.WorkingDirectory = new FileInfo(source).Directory.Name;
            shortcut.Save();
        }
    }
}

The problem is that, as an Administrator user, I want to create this shortcut on another user's Desktop. I could change the desktop string to be their Desktop path instead of mine, but the catch is this user will have been created right before I want to call the code to make a Desktop shortcut, so there is no C:\Users\TheUser folder yet!

What are some ways to alleviate this situation or make it possible to put a shortcut on a newly-created user's desktop? I preferably want to do this before the users actually log in for the first time. Thank you.

Community
  • 1
  • 1
Talset
  • 61
  • 2
  • 18
  • 1
    If you can't put in on the Public desktop, you can't do it. As you pointed out, the user's desktop won't exist before they logon. The best you can do is a login script or a RunOnce to create it as they logon. – NetMage Jan 26 '18 at 21:10
  • I did some looking into RunOnce, but it seems that can only be applied computer-wide and not for when a particular user logs on? Do you have a reference you can link? – Talset Jan 27 '18 at 00:53
  • You can do `RunOnce` for a user, but again, only after the registry hive for a user is created, which means only after they have logged on. If you potentially have multiple users on the computer, you will need to do a `Run` and look for the user you want to modify. – NetMage Jan 27 '18 at 01:27

0 Answers0