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

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

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory">Creating在目录应用程序快捷方式

Possible Duplicate:
Creating application shortcut in a directory

有很多的code左右浮动,展示了如何在.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(<一href="http://stackoverflow.com/questions/3037294/create-shortcut-from-vb-net-on-windows-7-box-64-bit">Create从vb.net在Windows 7中(64位))的快捷方式,但它不是对这个问题的正确答案。现在的问题是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".

推荐答案

您不必使用特殊的库来创建快捷方式,可以直接使用SHELL32自动化对象从C#或VB.NET程序。开始使用项目+添加引用,浏览选项卡中,选择C:\ WINDOWS \ SYSTEM32 \ shell32.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:\windows\system32\shell32.dll

然后写code这样创建.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) + @"\notepad.exe";
    lnk.Description = "Anything goes here";
    lnk.Arguments = @"c:\sample.txt";
    lnk.WorkingDirectory = @"c:\";

    // And dummy an icon (it will use notepad's)
    lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System) + "cmd.exe", 1);

    // Done, save it
    lnk.Save(lnkPath);

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

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