【问题标题】:Creating a shortcut file from Java从 Java 创建快捷方式文件
【发布时间】:2012-10-30 19:02:22
【问题描述】:

一个困扰我数周的问题是如何从 Java 创建快捷方式文件。现在在我说别的之前,我已经浏览了整个谷歌(以及这个网站;包括这个:Creating shortcut links (.lnk) from Java),试图找到一些有用的东西。我需要的不是创建快捷方式的安装程序包,而是从代码创建快捷方式。我所说的快捷方式是指通常在桌面上找到的 .lnk 文件。

我发现一个有用的东西是这个程序:

Java 代码:

import java.io.*;       
public class WindowsUtils {     
     private WindowsUtils() { }
     private static final String WINDOWS_DESKTOP = "Desktop";
     public static String getWindowsCurrentUserDesktopPath() { //return the current user desktop path
         return System.getenv("userprofile") + "/" + WINDOWS_DESKTOP ;
     }
     public static void createInternetShortcutOnDesktop(String name, String target) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, "");
     }
     public static void createInternetShortcutOnDesktop(String name, String target, String icon) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, icon);
     }
     public static void createInternetShortcut(String name, String where, String target, String icon) throws IOException {
         FileWriter fw = new FileWriter(where);
         fw.write("[InternetShortcut]\n");
         fw.write("URL=" + target + "\n");
         if (!icon.equals("")) {
             fw.write("IconFile=" + icon + "\n");*
         }
         fw.flush();
         fw.close();
     }
     public static void main(String[] args) throws IOException {
         WindowsUtils.createInternetShortcutOnDesktop("GOOGLE", "http://www.google.com/");
     }
}

我玩弄了它,并设法在我的桌面上创建了一个 .lnk 快捷方式。但是,我发现了两个问题:

我无法更改图标,尽管路径将其链接到正确的图标。 我创建了一条通往 C:/Users/USER/Documents 的路径,但是,每当我单击快捷方式时,它就会将我带到 C:/。当我删除快捷方式时,对话框确实显示路径是 file:////C:/Users/USER/Documents。

我知道上面的这段代码最初是为了创建 Internet 快捷方式,所以我相信我可能采取了错误的方法。如果您能给我提供任何帮助/链接,我将不胜感激。

【问题讨论】:

标签: java windows


【解决方案1】:

我可以在 GitHub 上推荐这个存储库:

https://github.com/BlackOverlord666/mslinks

在那里,我找到了 create 快捷方式的简单解决方案:

ShellLink.createLink("path/to/existing/file.txt", "path/to/the/future/shortcut.lnk");

如果你想阅读快捷键:

File shortcut = ...;
String pathToExistingFile = new ShellLink(shortcut).resolveTarget();

如果您想更改快捷方式的图标,请使用:

ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");

希望对你有帮助:)

亲切的问候

约书亚·弗兰克

【讨论】:

    【解决方案2】:

    我知道我参加聚会有点晚了,我真的很喜欢上面显示的 JShellLink 实现,但是如果你需要一些更简单的东西,那么我写的是:
    https://github.com/JacksonBrienen/VBS-Shortcut/blob/main/src/vbs_sc/ShortcutFactory.java
    应该做的伎俩。总而言之,它是一个简单的快捷方式创建器,使用 VBS 通过 CMD 运行。
    使用此功能后,创建桌面快捷方式将非常简单:

    ShortcutFactory.createDesktopShortcut("C:/Program Files/Internet Explorer/iexplore.exe", "Shortcut.lnk");
    

    【讨论】:

      【解决方案3】:

      http://www.mindfiresolutions.com/Creating-shortcut-from-a-Java-Application-1712.php

      一个不错的简单教程,使用名为 jShellLink 的库来使用 jni

      【讨论】:

        猜你喜欢
        • 2013-08-04
        • 2016-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 2016-05-15
        • 1970-01-01
        相关资源
        最近更新 更多