如何在C#中以编程方式创建桌面快捷方式(链接),但使用自己的快捷方式文件扩展名(例如.appfolder)而不是.lnk?
我已在注册表中注册了名为.appfolder的快捷方式文件扩展名: 它的行为应该与普通的.lnk快捷方式类似,但必须以.appfolder结尾(这是有原因的) 现在我想创建一个快捷方式,该文件以C#的编程方式结尾/扩展名。 .lnk的默认值结束它的工作方式:如何在C#中以编程方式创建桌面快捷方式(链接),但使用自己的快捷方式文件扩展名(例如.appfolder)而不是.lnk?,c#,registry,desktop,shortcut,file-extension,C#,Registry,Desktop,Shortcut,File Extension,我已在注册表中注册了名为.appfolder的快捷方式文件扩展名: 它的行为应该与普通的.lnk快捷方式类似,但必须以.appfolder结尾(这是有原因的) 现在我想创建一个快捷方式,该文件以C#的编程方式结尾/扩展名。 .lnk的默认值结束它的工作方式: public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation) { str
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");
当我尝试更改此行中的文件扩展名时string shortcutLocation=Path.Combine(shortcutPath,shortcutName+@.lnk”)
to.appfolder我得到一个System.Runtime.InteropServices.COMException
(HRESULT:0x80020009(DISP\u E\u异常)
)
使用file.Move(shortcutLocation,Path.ChangeExtension(shortcutLocation,.appfolder)),创建/保存快捷方式后尝试更改文件扩展名时代码>我在桌面上获得具有以下属性的文件:
正如您所见,Windows确实将.appfolder扩展名识别为快捷方式,但“快捷方式”选项卡丢失,当我双击要执行的文件时,不会发生任何情况
因此,我的.appfolder注册不完整,尽管它应该是。。。或者如何在C#中以编程方式创建具有自己的快捷方式文件扩展名的快捷方式
谢谢你的帮助;)
编辑:
我尝试了另一种通过编程创建快捷方式的方法:
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);
}
它创建快捷方式文件时没有任何错误(如System.Runtime.InteropServices.COMException
,请参见上文),但它不会通过双击这两个文件来执行.exe文件。我解决了它:
我对.appfolder的注册不完全正确,无法完全像.lnk快捷方式文件那样工作。
遵循指示。
在这之后,C#中的两种方法都起到了上述作用。我解决了它:
我对.appfolder的注册不完全正确,无法完全像.lnk快捷方式文件那样工作。
遵循指示。
在这之后,上面提到的C#中的两种方法都起作用。您是否尝试手动创建.appfolder快捷方式?它有用吗?您可以注册协议处理程序,如http://
并使用appfolder://
协议处理程序。然后,您可以创建普通的.lnk
文件,该文件具有自定义的appfolder://
link@oleksa如何手动创建.appfolder快捷方式?创建normal.lnk和通过记事本重命名为.appfolder都不起作用,与File.Move(…)相同。或者你是什么意思?很遗憾,您建议的URL解决方案不适用于我的应用程序。它必须是.appfolder扩展名。如果无法手动创建工作.appfolder
快捷方式,则必须找到其他方法。在这种情况下,这不是编程问题。但是自定义协议处理程序是一个工作特性(我已经使用过),它可以用appfolder://
链接作为参数启动应用程序,比如c:\app\customhandler.exeappfolder://notepad.exe%20%or%20%notepad++.exe
随便什么。如果您尝试手动创建.appfolder快捷方式,这可能对您有用?它有用吗?您可以注册协议处理程序,如http://并使用appfolder://
协议处理程序。然后,您可以创建普通的.lnk
文件,该文件具有自定义的appfolder://
link@oleksa如何手动创建.appfolder快捷方式?创建normal.lnk和通过记事本重命名为.appfolder都不起作用,与File.Move(…)相同。或者你是什么意思?很遗憾,您建议的URL解决方案不适用于我的应用程序。它必须是.appfolder扩展名。如果无法手动创建工作.appfolder
快捷方式,则必须找到其他方法。在这种情况下,这不是编程问题。但是自定义协议处理程序是一个工作特性(我已经使用过),它可以用appfolder://
链接作为参数启动应用程序,比如c:\app\customhandler.exeappfolder://notepad.exe%20%or%20%notepad++.exe
随便什么。也许这对你有用