在 .Net 中为 64 位机器创建快捷方式 - 仅编译为 64 位应用程序 [英] Create Shortcut in .Net for 64-Bit Machines - Compiled as 64-bit Application Only

查看:31
本文介绍了在 .Net 中为 64 位机器创建快捷方式 - 仅编译为 64 位应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在目录中创建应用程序快捷方式

有很多代码展示了如何在 .Net 中创建快捷方式,但它仅在编译为 32 位应用程序时才有效.您不能在 64 位应用程序中使用 IWshRuntimeLibrary.WshShell.

There is a lot of code floating around showing how to create a shortcut in .Net, but it only works when compiled as a 32 bit application. You can't use IWshRuntimeLibrary.WshShell in a 64 bit application.

有谁知道如何在 64 位应用程序中创建快捷方式?

Does anyone know how to create short-cuts in 64 bit applications?

请注意,我也不是在寻找在安装时执行此操作的方法.这是用于安装后的目的.

Note, I'm not looking for a way to do it while installing either. This is for post-install purposes.

我知道这篇关于 SO (在 Windows 7 盒子(64 位)上从 vb.net 创建快捷方式),但这不是问题的正确答案.问题是 64 位的,该人给出了 32 位的答案并说只编译 32 位".

And I'm aware of this post on SO (Create shortcut from vb.net on Windows 7 box (64 bit)), but it's not the correct answer for the question. The question is 64-bit and the person gave a 32-bit answer and said "just compile 32-bit".

推荐答案

您不需要使用特殊的库来创建快捷方式,您可以直接从 C# 或 VB.NET 程序使用 Shell32 自动化对象.开始使用项目 + 添加引用,浏览选项卡,选择 c:windowssystem32shell32.dll

You don't need to use special libraries to create the shortcut, you can use the Shell32 automation object directly from a C# or VB.NET program. Get started with Project + Add Reference, Browse tab, select c:windowssystem32shell32.dll

然后编写这样的代码来创建.lnk文件:

Then write code like this to create the .lnk file:

    // 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) + @"
otepad.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);

这篇关于在 .Net 中为 64 位机器创建快捷方式 - 仅编译为 64 位应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆