71

如何使用 C# 或使用 .NET 框架创建应用程序快捷方式(.lnk 文件)?

结果将是一个指向指定应用程序或 URL 的 .lnk 文件。

4

6 回答 6

61

它不像我想要的那么简单,但是在 vbAccelerator 上有一个很棒的类 叫做ShellLink.cs

此代码使用互操作,但不依赖 WSH。

使用这个类,创建快捷方式的代码是:

private static void configStep_addShortcutToStartupGroup()
{
    using (ShellLink shortcut = new ShellLink())
    {
        shortcut.Target = Application.ExecutablePath;
        shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
        shortcut.Description = "My Shorcut Name Here";
        shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
        shortcut.Save(STARTUP_SHORTCUT_FILEPATH);
    }
}
于 2008-10-24T17:38:42.353 回答
54

干净整洁。( .NET 4.0 )

Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
dynamic shell = Activator.CreateInstance(t);
try{
    var lnk = shell.CreateShortcut("sc.lnk");
    try{
        lnk.TargetPath = @"C:\something";
        lnk.IconLocation = "shell32.dll, 1";
        lnk.Save();
    }finally{
        Marshal.FinalReleaseComObject(lnk);
    }
}finally{
    Marshal.FinalReleaseComObject(shell);
}

就是这样,不需要额外的代码。CreateShortcut甚至可以从文件中加载快捷方式,因此TargetPath等属性会返回现有信息。快捷方式对象属性

对于不支持动态类型的 .NET 版本,也可以采用这种方式。( .NET 3.5 )

Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
object shell = Activator.CreateInstance(t);
try{
    object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[]{"sc.lnk"});
    try{
        t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[]{@"C:\whatever"});
        t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[]{"shell32.dll, 5"});
        t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null);
    }finally{
        Marshal.FinalReleaseComObject(lnk);
    }
}finally{
    Marshal.FinalReleaseComObject(shell);
}
于 2013-11-11T19:13:02.650 回答
13

我发现了这样的事情:

private void appShortcutToDesktop(string linkName)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=file:///" + app);
        writer.WriteLine("IconIndex=0");
        string icon = app.Replace('\\', '/');
        writer.WriteLine("IconFile=" + icon);
        writer.Flush();
    }
}

悲伤人文章“url-link-to-desktop”中的原始代码

于 2010-04-29T11:17:00.237 回答
1

下载 IWshRuntimeLibrary

您还需要导入 COM 库IWshRuntimeLibrary。右键单击您的项目 -> 添加引用 -> COM -> IWshRuntimeLibrary -> 添加,然后使用以下代码片段。

private void createShortcutOnDesktop(String executablePath)
{
    // Create a new instance of WshShellClass

    WshShell lib = new WshShellClass();
    // Create the shortcut

    IWshRuntimeLibrary.IWshShortcut MyShortcut;


    // Choose the path for the shortcut
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    MyShortcut = (IWshRuntimeLibrary.IWshShortcut)lib.CreateShortcut(@deskDir+"\\AZ.lnk");


    // Where the shortcut should point to

    //MyShortcut.TargetPath = Application.ExecutablePath;
    MyShortcut.TargetPath = @executablePath;


    // Description for the shortcut

    MyShortcut.Description = "Launch AZ Client";

    StreamWriter writer = new StreamWriter(@"D:\AZ\logo.ico");
    Properties.Resources.system.Save(writer.BaseStream);
    writer.Flush();
    writer.Close();
    // Location for the shortcut's icon           

    MyShortcut.IconLocation = @"D:\AZ\logo.ico";


    // Create the shortcut at the given path

    MyShortcut.Save();

}
于 2013-12-20T03:49:21.797 回答
1

在调查了我在 SO 上发现的所有可能性后,我选择了ShellLink

//Create new shortcut
using (var shellShortcut = new ShellShortcut(newShortcutPath)
{
     Path = path
     WorkingDirectory = workingDir,
     Arguments = args,
     IconPath = iconPath,
     IconIndex = iconIndex,
     Description = description,
})
{
    shellShortcut.Save();
}

//Read existing shortcut
using (var shellShortcut = new ShellShortcut(existingShortcut))
{
    path = shellShortcut.Path;
    args = shellShortcut.Arguments;
    workingDir = shellShortcut.WorkingDirectory;
    ...
}

除了简单有效之外,作者(Mattias Sjögren,MS MVP)还是某种 COM/PInvoke/Interop 大师,仔细阅读他的代码,我相信它比其他替代方案更强大。

应该提到的是,快捷方式文件也可以由几个命令行实用程序创建(反过来可以很容易地从 C#/.NET 调用)。我从未尝试过其中任何一个,但我会从NirCmd开始(NirSoft 有类似 SysInternals 的质量工具)。

不幸的是,NirCmd 无法解析快捷方式文件(仅创建它们),但为此目的,TZWorks lp似乎有能力。它甚至可以将其输出格式化为 csv。lnk-parser看起来也不错(它可以输出 HTML 和 CSV)。

于 2014-05-24T18:03:26.483 回答
0

IllidanS4 的回答类似,使用Windows Script Host对我来说是最简单的解决方案(在 Windows 8 64 位上测试)。

但是,与其通过代码手动导入 COM 类型,不如将 COM 类型库添加为参考更容易。选择References->Add Reference...COM->Type Libraries找到并添加“Windows Script Host Object Model”

这将导入命名空间IWshRuntimeLibrary,您可以从中访问:

WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkPathName);
link.TargetPath=TargetPathName;
link.Save();

归功于 Jim Hollenhorst

于 2015-02-09T19:06:37.933 回答