1

I have registered a shortcut file extension named .appfolder in registry: reg export

It should behave like a normal .lnk shortcut, but has to have .appfolder ending (that has it reasons).

Now I want to create an shortcut with that file ending/extension programmatically in C#. Default for .lnk ending it works with:

public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
    {
        string shortcutLocation = Path.Combine(shortcutPath, shortcutName + @".lnk");
        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
        shortcut.Description = "My shortcut description";
        shortcut.TargetPath = targetFileLocation;                 
        shortcut.Save();
    }

//e.g:
CreateShortcut("Example (Notepad)", Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "notepad.exe");

When I try to change file extension in this line string shortcutLocation = Path.Combine(shortcutPath, shortcutName + @".lnk"); to .appfolder I get an System.Runtime.InteropServices.COMException (HRESULT: 0x80020009 (DISP_E_EXCEPTION)).

When I try to change file extension after creating/saving shortcut with File.Move(shortcutLocation, Path.ChangeExtension(shortcutLocation, ".appfolder")); I get an file on desktop with following properties: image

As you can see Windows does recognize .appfolder extension as shortcut, but the 'Shortcut' tab is missing and when I double-click the file to execute, it happens nothing.

So, either my registration of .appfolder is not complete right, although it should be... or how to create a shortcut with own shortcut file extension programmatically in C#?

Thanks for help ;)

EDIT :

I tried another way to create the shortcut programatically:

void main() {
    IShellLink link = (IShellLink)new ShellLink();

    // setup shortcut information
    link.SetDescription("My Description");
    link.SetPath(@"notepad.exe");

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

    [ComImport]
    [Guid("00021401-0000-0000-C000-000000000046")]
    internal class ShellLink
    {
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("000214F9-0000-0000-C000-000000000046")]
    internal interface IShellLink
    {
        void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
        void GetIDList(out IntPtr ppidl);
        void SetIDList(IntPtr pidl);
        void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
        void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
        void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
        void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
        void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
        void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
        void GetHotkey(out short pwHotkey);
        void SetHotkey(short wHotkey);
        void GetShowCmd(out int piShowCmd);
        void SetShowCmd(int iShowCmd);
        void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
        void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
        void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
        void Resolve(IntPtr hwnd, int fFlags);
        void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
    }

It creates the shortcut file without any error (like System.Runtime.InteropServices.COMException, see above), but it doesn't execute the .exe file by double-clicking neither.

  • had you tried to create the .appfolder shortcut manually? Does it work? You can register protocol handler, like `http://` and use [the registry modification to add your own](https://superuser.com/questions/1049803/register-custom-uri-scheme-handler-in-windows-10) `appfolder://` protocol handler. Then you can create ordinary `.lnk` file that has custom `appfolder://` link – oleksa Jan 05 '20 at 16:38
  • @oleksa How do I create the .appfolder shortcut manually? Creating normal .lnk and renaming to .appfolder via notepad works neither, same as File.Move(...). Or what do you mean? Your suggested solution with URL unfortunately doesn't work for my application. It has to be the .appfolder extension. – Blood_Working_ Jan 05 '20 at 16:53
  • well, you have to find another way if it is not possible to create working `.appfolder` shortcut manually. It is not programming question in such case. But custom protocol handler is a working feature (I've used it) that can start your application with `appfolder://` link as a parameter, like `c:\app\customhandler.exe appfolder://notepad.exe%20%or%20%notepad++.exe` whatever. Probably this will work for you – oleksa Jan 05 '20 at 17:09

1 Answers1

0

I solved it: My registration of .appfolder wasn't completely correct to work exactly like .lnk shortcut file. Follow this instruction. After that both methods in C# mentioned above work.