What should I put in link.SetPath? to create shortcut for winui3 app

118 views Asked by At

How can I create a shortcut programmatically for my WinUI 3 app written in C#? I've tried some code that I've found on this website(https://stackoverflow.com/a/14632782/10228444)

private static void CreateLink()
{
    IShellLink link = (IShellLink)new ShellLink();

    // setup shortcut information
    link.SetDescription("My Description");
    string installedPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    link.SetPath(installedPath);

    // save it
    IPersistFile file = (IPersistFile)link;
    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    file.Save(Path.Combine(desktopPath, "MyLink.lnk"), false);
}

installedPath references C:Users\Program Files\WindowsApps\GUID_1.0.0_x64_hash\MyApp.dll. I've also tried with ...\MyApp.exe but to no avail.
I've created manualy a shortcut for the app and it's properties are different from other shortcuts. TargetType would be GUID__hash(instead of GUID was the value that I've obtained for GUID in installedPath variable, same for hash), TargetType Applications and Target GUID__hash!App. An excel shortcut would contain as TargetType Application, TargetLocation Office 16, Target "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE".

2

There are 2 answers

2
Jeaninez - MSFT On

If your project is a unpackage project, you could double click the exe. You could try to create a desktop shortcut with the Windows Script Host

If your project is a package project, you need to use the Windows Application Packaging Project to generate an MSIX for your application. And you couldn't create a desktop shortcut from code for the project

0
7Mircea On

I found that there is a way. Put the first part of the folder where your app is installed and the hash from the ending in a string called location. Then set the path as "shell:AppsFolder\location!App". And that's it.

public static void CreateLink()
{
    if (!WasShortcutCreated())
    {
        IShellLinkW link = (IShellLinkW)new ShellLink();
        //Get app location
        string imageLocation = string.Empty;
        string location = GetAppLocation(ref imageLocation);
        
        // setup shortcut information
        link.SetDescription($"Shortcut for {Constants.AppName}");
        link.SetPath(@"shell:AppsFolder\" + location + @"!App");
        link.SetIconLocation(Path.Combine(imageLocation, @"Assets\Images\AppIcon\icn_app.ico"), 0);

        // save it
        IPersistFile file = (IPersistFile)link;
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        file.Save(System.IO.Path.Combine(desktopPath, $"{Constants.AppName}.lnk"), false);
      }

}
private static string GetAppLocation(ref string imageLocation)
{
    string location = string.Empty;
    try
    {
        location = System.Reflection.Assembly.GetExecutingAssembly().Location;
        //extract the  folder name identity_version_configuration_hash
        int lastBackslash = location.LastIndexOf('\\');
        if (lastBackslash == -1) return location;
        location = location.Substring(0, lastBackslash);
        imageLocation = location;
        lastBackslash = location.LastIndexOf('\\');
        if (lastBackslash == -1 || lastBackslash == location.Length - 1) return location;
        location = location.Substring(lastBackslash + 1);
        //remove from folder name version and configuration
        int start = location.IndexOf('_');
        int end = location.LastIndexOf('_');
        if (start == -1 || end == -1) return location;
        string firtPartOfLocation = location.Substring(0, start);
        string lastPartOfLocation = location.Substring(end);
        location = firtPartOfLocation + lastPartOfLocation;
    }
    catch (Exception e)
    {
        //TODO manage exception
    }
    return location;
}

To learn how to use IShellLink check this IShellLink.