gpt4 book ai didi

java - 如何使用 ProcessBuilder 在 Java 中启动带空格的快捷方式

转载 作者:行者123 更新时间:2023-11-30 11:41:09 29 4
gpt4 key购买 nike

这是我通常用来执行快捷方式文件 (.lnk) 的代码

  //Get Directory
String currentDir = new File(game.getGamePath()).getCanonicalPath();

//Parse Directory to put apostrophes around shortcut name
currentDir = currentDir.substring(0, currentDir.lastIndexOf("\\") + 1) + '"' +
currentDir.substring(currentDir.lastIndexOf("\\") + 1, currentDir.length()) + '"';

//prep the launcher
ProcessBuilder processBuild = new ProcessBuilder();
processBuild.command("cmd", "/c", "start" ,"/wait", "", currentDir);

//launch
Process = processBuild.start();
try {
Process.waitFor();
} catch (InterruptedException ex) {

}

问题是当快捷方式文件名有空格时,我从 Windows 收到错误提示它无法加载 [WordAfterSpace.ink]

例如,假设我有一个值为 [Desktop\A B.lnk] 的 currentDir

解析为 [Desktop\"A B.ink"],这在命令提示符下完美运行。

问题是如果我使用上面的代码我会得到这个错误:

Windows cannot Find 'B.ink', Make sure you typed the name in correctly and try again

最佳答案

使用\"获取字符串中的双引号另外,从一开始就引用整个链接,而不是仅仅引用链接名称,因为它前面可能还有其他空格。这将使您免于麻烦。

关于java - 如何使用 ProcessBuilder 在 Java 中启动带空格的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379617/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com