可能的重复:
在目录中创建应用程序快捷方式 https://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory
有大量代码展示了如何在 .Net 中创建快捷方式,但它仅在编译为 32 位应用程序时才有效。您无法在 64 位应用程序中使用 IWshRuntimeLibrary.WshShell。
有谁知道如何在 64 位应用程序中创建快捷方式?
请注意,我并不是在安装时寻找一种方法。这是为了安装后的目的。
我知道这篇关于 SO 的文章(在 Windows 7 盒子(64 位)上从 vb.net 创建快捷方式 https://stackoverflow.com/questions/3037294/create-shortcut-from-vb-net-on-windows-7-box-64-bit),但这不是问题的正确答案。问题是64位的,对方给出了32位的答案,并说“只编译32位”。
您不需要使用特殊的库来创建快捷方式,您可以直接从 C# 或 VB.NET 程序使用 Shell32 自动化对象。开始使用项目 + 添加引用,浏览选项卡,选择 c:\windows\system32\shell32.dll
然后编写如下代码来创建 .lnk 文件:
// Creating a link named "test" on the desktop
string lnkDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string lnkName = "test";
// Create an empty .lnk file so we can create an object for it
string lnkPath = System.IO.Path.Combine(lnkDir, lnkName) + ".lnk";
System.IO.File.WriteAllBytes(lnkPath, new byte[] { });
// Initialize a ShellLinkObject for that .lnk file
Shell32.Shell shl = new Shell32.ShellClass();
Shell32.Folder dir = shl.NameSpace(lnkDir);
Shell32.FolderItem itm = dir.Items().Item(lnkName + ".lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// We'll just dummy a link to notepad
lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
lnk.Description = "Anything goes here";
lnk.Arguments = @"c:\sample.txt";
lnk.WorkingDirectory = @"c:\";
// And dummy an icon (it will the one used by cmd.exe)
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System) + "cmd.exe", 1);
// Done, save it
lnk.Save(lnkPath);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)