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.
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.
User contributions licensed under CC BY-SA 3.0