从Java创建快捷方式文件 [英] Creating a shortcut file from Java

查看:68
本文介绍了从Java创建快捷方式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个困扰我数周的问题是如何从Java创建快捷方式文件.现在,在我再说什么之前,我已经浏览了整个Google(以及本网站;包括以下内容:

One problem which has been puzzling me for weeks now is how I can create a shortcut file from Java. Now before I say anything else, I have looked all over Google (and also on this site; including this: Creating shortcut links (.lnk) from Java) trying to find something helpful. What I need isn't an installer package which creates a shortcut, but to create a shortcut from code. What I mean by shortcut is a .lnk file which are usually found on the Desktop.

我发现有帮助的事情之一是该程序:

One of the helpful things I found was this program:

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快捷方式.但是,我遇到两个问题:

I toyed around with it, and managed to create a .lnk shortcut on my desktop. However, I foudn two problems:

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

I couldn't change the icon, despite the path linking it to a correct icon. I made a path which led me to C:/Users/USER/Documents, however, whenever I clicked the shortcut it took me to C:/. When I delete the shortcut, the dialogue shows me indeed that the path is file:////C:/Users/USER/Documents.

我知道上面的这段代码最初是用于创建Internet快捷方式的,所以我认为我可能采取了错误的方法.我会很感激您能给我的任何帮助/链接.

I know that this code above was originally meant to create Internet Shortcuts, so I believe I might have taken the wrong approach. I would appreciate any help/links you can give me.

推荐答案

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

https://github.com/BlackOverlord666/mslinks

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

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

如果您想阅读快捷方式:

I can recommend this repository on GitHub:

https://github.com/BlackOverlord666/mslinks

There I've found a simple solution to create shortcuts:

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

If you want to read shortcuts:

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

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

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

希望这对您有帮助:)

亲切的问候

乔苏阿·弗兰克(Josua Frank)

Hope this helps you :)

Kind regards

Josua Frank

这篇关于从Java创建快捷方式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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