使用 C# 代码创建快捷方式文件

栏目: ASP.NET · 发布时间: 7年前

内容简介:快捷方式是一种特殊的文件,扩展名为 lnk。有很多种方式来创建快捷方式,不过使用 C# 代码创建一个却并不那么容易。本文分享三种不同的方式创建快捷方式。

快捷方式是一种特殊的文件,扩展名为 lnk。有很多种方式来创建快捷方式,不过使用 C# 代码创建一个却并不那么容易。

本文分享三种不同的方式创建快捷方式。

随处可用的代码

这是最方便的方式了,因为这段代码随便放到一段代码中就能运行:

/// <summary>
/// 为当前正在运行的程序创建一个快捷方式。
/// </summary>
/// <param name="lnkFilePath">快捷方式的完全限定路径。</param>
/// <param name="args">快捷方式启动程序时需要使用的参数。</param>
private static void CreateShortcut(string lnkFilePath, string args = "")
{
    var shellType = Type.GetTypeFromProgID("WScript.Shell");
    dynamic shell = Activator.CreateInstance(shellType);
    var shortcut = shell.CreateShortcut(lnkFilePath);
    shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
    shortcut.Arguments = args;
    shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    shortcut.Save();
}

以上代码为当前正在运行的程序创建一个快捷方式。当然,如果你希望给其他文件创建快捷方式,就改一改里面的代码吧,将 TargetPathWorkingDirectory 改为其他参数。

使用 C# 代码创建快捷方式文件 ▲ 快捷方式属性(其中 Target 等同于上面的 TargetPathArguments 一起,Start in 等同于上面的 WorkingDirectory

引用 COM 组件

引用 COM 组件 Interop.IWshRuntimeLibrary.dll 能够获得类型安全,不过本质上和以上方法是一样的。

private static void CreateShortcut(string lnkFilePath, string args = "")
{
    var shell = new IWshRuntimeLibrary.WshShell();
    var shortcut = (IWshRuntimeLibrary.IWshShortcut) shell.CreateShortcut(linkFileName);
    shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
    shortcut.Arguments = args;
    shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    shortcut.Save();
}

兼容 .NET 3.5 或早期版本

如果你还在使用 .NET Framework 3.5 或更早期版本,那真的很麻烦。同情你以下,不过也贴一段代码:

private static void CreateShortcut(string lnkFilePath, string args = "")
{
    var shellType = Type.GetTypeFromProgID("WScript.Shell");
    var shell = Activator.CreateInstance(shellType);
    var shortcut = shellType.InvokeMember("CreateShortcut",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
        null, shell, new object[] { linkFileName });
    var shortcutType = shortcut.GetType();
    shortcutType.InvokeMember("TargetPath",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
        null, shortcut, new object[] { Assembly.GetEntryAssembly().Location });
    shortcutType.InvokeMember("Arguments",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, 
        null, shortcut, new object[] { args });
    shortcutType.InvokeMember("WorkingDirectory",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, 
        null, shortcut, new object[] { AppDomain.CurrentDomain.SetupInformation.ApplicationBase });
    shortcutType.InvokeMember("Save",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
        null, shortcut, null);
}

本文会经常更新,请阅读原文: https://walterlv.github.io/post/create-shortcut-file-using-csharp.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

使用 C# 代码创建快捷方式文件 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Modeling the Internet and the Web

Modeling the Internet and the Web

Pierre Baldi、Paolo Frasconi、Padhraic Smyth / Wiley / 2003-7-7 / USD 115.00

Modeling the Internet and the Web covers the most important aspects of modeling the Web using a modern mathematical and probabilistic treatment. It focuses on the information and application layers, a......一起来看看 《Modeling the Internet and the Web》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具