简体   繁体   English

如何在 C# 中以编程方式创建桌面快捷方式(链接),但使用自己的快捷方式文件扩展名(例如 .appfolder)而不是 .lnk?

[英]How to create a desktop shortcut (link), but with own shortcut file extension (e.g. .appfolder) instead of .lnk programmatically in C#?

I have registered a shortcut file extension named .appfolder in registry: reg export我在注册表中注册了一个名为 .appfolder 的快捷方式文件扩展名: reg export

It should behave like a normal .lnk shortcut, but has to have .appfolder ending (that has it reasons).它应该表现得像一个普通的 .lnk 快捷方式,但必须以 .appfolder 结尾(这是有原因的)。

Now I want to create an shortcut with that file ending/extension programmatically in C#.现在我想在 C# 中以编程方式创建一个带有该文件结尾/扩展名的快捷方式。 Default for .lnk ending it works with: .lnk 结尾的默认值适用于:

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");当我尝试更改此行string shortcutLocation = Path.Combine(shortcutPath, shortcutName + @".lnk");文件扩展名时string shortcutLocation = Path.Combine(shortcutPath, shortcutName + @".lnk"); to .appfolder I get an System.Runtime.InteropServices.COMException ( HRESULT: 0x80020009 (DISP_E_EXCEPTION) ).到 .appfolder 我得到一个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"));当我在使用File.Move(shortcutLocation, Path.ChangeExtension(shortcutLocation, ".appfolder"));创建/保存快捷方式后尝试更改文件扩展名时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.正如您所看到的,Windows 确实将 .appfolder 扩展名识别为快捷方式,但缺少“快捷方式”选项卡,当我双击该文件以执行时,它什么也没发生。

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#?所以,要么我的 .appfolder 注册不完整,虽然它应该是......或者如何在 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.它创建没有任何错误的快捷方式文件(如System.Runtime.InteropServices.COMException ,见上文),但它不会通过双击两者来执行 .exe 文件。

I solved it: My registration of .appfolder wasn't completely correct to work exactly like .lnk shortcut file.我解决了它:我的 .appfolder 注册并不完全正确,无法像 .lnk 快捷方式文件一样工作。 Follow this instruction.请遵循说明。 After that both methods in C# mentioned above work.之后,上面提到的 C# 中的两种方法都可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM