从Java创建快捷方式文件

10
有一个问题已经困扰我数周,那就是如何从Java创建快捷方式文件。在我说其他任何事情之前,我已经在Google上搜索过所有内容(包括此网站:Creating shortcut links (.lnk) from Java),试图找到一些有用的东西。我需要的不是创建快捷方式的安装程序包,而是从代码中创建快捷方式。我指的快捷方式是通常在桌面上找到的.lnk文件。
我发现的一些有用的东西之一是这个程序:
Java Code:
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快捷方式。然而,我发现了两个问题:
1.尽管路径正确,但我无法更改图标。 2.我创建了一个指向C:/Users/USER/Documents的路径,但是每当我点击快捷方式时,它会带我到C:/。当我删除快捷方式时,对话框显示路径确实为file:////C:/Users/USER/Documents。
我知道这段代码最初是用于创建Internet快捷方式的,所以我认为我可能采取了错误的方法。如果您能给我任何帮助/链接,我将不胜感激。

看一下这个库:http://alumnus.caltech.edu/~jimmc/jshortcut/ - Afshin Moazami
3个回答

14
我可以推荐GitHub上的这个代码库:https://github.com/BlackOverlord666/mslinks。在那里,我找到了一个简单的解决方案,用于创建快捷方式: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");

希望这能对你有所帮助 :)

祝好

Josua Frank


1

我知道我有点晚来到这个派对,我真的很喜欢上面展示的JShellLink实现,但如果你需要更简单的东西,那么我写的:
ShortcutFactory.java
应该就可以了。总之,它是一个简单的快捷方式创建器,使用CMD运行VBS。
一旦使用这个工具,创建桌面快捷方式就像这样简单:

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

谢谢你的代码,Jackson。我刚刚测试了一下,它运行得非常好。我敢提出一个增强请求:你能否添加设置用户指定图标到链接的可能性? - Jörg
虽然我完全不了解VBS,但我找到了一种添加图标的方法<pre>- 在createShortcut(String source, String linkPath)中添加第三个参数String iconPath <br/> - 在+ &quot;\tscObj.TargetPath = \&quot;%s\&quot;%n&quot;后插入新行+ &quot;\tscObj.IconLocation = \&quot;%s\&quot;%n&quot; <br/> - 在linkPath, source后追加, icon</pre> - Jörg
嘿@Jörg,我已经添加到Github页面,并为其创建了发布二进制文件。如果您有任何问题,请告诉我。 - Jackson Brienen
太棒了。再次感谢@Jackson。由于一切都运行正常,我目前没有问题,但还是谢谢你的提供。不过,你可以更新当前形式下Java20弃用的Process p = Runtime.getRuntime() ...这一行。 - Jörg
只需将命令作为字符串数组传递,一切都很好。 - Jörg

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接