Search code examples
c#shortcutlnk

How to create a shortcut without admin privileges in C#?


I want to create a .lnk shortcut to a .exe file on desktop but I can't run my program as admin.

Using Windows Script Host Object does not work for me, because I get the Task could not find “AxImp.exe” error. This problem could be solved by installing Windows SDK, but that also needs admin rights to be installed.

Is there a way in C# to create a .lnk shortcut without admin rights (whether third party or build-in doesn't matter)?


Solution

  • If you want to create shortcut in current users's desktop and have UAC problem and cant't run as administrator, you can create a script.vbs in your project and set it to copy in output directory, then run vbs when you need.

    Content of script.vbs:

    Dim WshShell
    Set WshShell = CreateObject("Wscript.shell")
    Dim strDesktop 
    strDesktop= WshShell.SpecialFolders("Desktop")
    Dim oMyShortcut
    Set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Shortcut to Notepad.lnk")
    OMyShortcut.TargetPath = "notepad.exe"
    oMyShortCut.Save
    Set WshShell= Nothing
    Set oMyShortcut= Nothing
    

    Run script.vbs:

    private void button1_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(System.IO.Path.Combine(Application.StartupPath, "script.vbs"));
    }