private void createDesktopInk(String sourcePath, String lnkName) {
// String sourcePath = "C:\\Program Files (x86)\\Notepad++\\notepad++.exe";
String desktopPath = System.getProperty("user.home") + "\\Desktop";
String linkPath = desktopPath + "\\" + lnkName + ".lnk";
File linkFile = new File(linkPath);
if (linkFile.exists()) {
linkFile.delete();
}
String adminShell = String.format("cmd /c mklink \"%s\" \"%s\"", linkPath, sourcePath);
try {
System.out.println(adminShell);
// 请求管理员权限并执行命令
Process p = Runtime.getRuntime().exec(adminShell);
String out = IOUtils.toString(p.getInputStream(), "gbk");
System.out.println(out);
String err = IOUtils.toString(p.getErrorStream(), "gbk");
System.out.println(err);
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建桌面快捷方式失败,请确保以管理员权限启动...");
}
}
Java创建桌面快捷方式
于 2023-05-25 17:10:06 首次发布