Вопрос

This is the code I would use normally to execute a Shortcut file (.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) {

  }

Problem is when the Shortcut file name has spaces I get an Error from windows saying it could not load [WordAfterSpace.ink]

For example say I had a currentDir with value [Desktop\A B.lnk]

Parsed it would be [Desktop\"A B.ink"] and this works perfectly on the command prompt.

problem is I would get this Error if I were to use the above code:

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

Это было полезно?

Решение

Use \" to get double quotes within strings Also, quote the whole link from the start instead of just the link name as you might have other spaces before it. That will save you from trouble.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top