-1

How to get the path of Shortcut i.e., lnk file when we read from FileChooser java

We have a environment that, actual files are stored in the server and shortcut files are given to users. User choose the shortcut file from the application, but when we choose the shortcut file, it reads the target file. Now, how to get the shortcut file path, FileChooser always get the target file path and also want to know how to identify the file is a shortcut file or regular file? as because FileChooser always points to target file.

This is the fileChooser used

FileChooser fileChooser = new FileChooser();

File file = fileChooser.showOpenDialog(argument);

here file always points to target file

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
  • `.lnk` is a full-fledged format that stores quite a lot of data. If your server generates those, look into their attributes and pick out where the data you need is located. Otherwise, here's the format description: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-shllink/16cb4ca1-9339-4d0c-a68d-bf1d6cc0f943 – M. Prokhorov Sep 18 '20 at 19:16
  • We are selecting the lnk file, but it is pointing to target file. Problem is that we need to find out shortcut file path, when we select the shortcut file, it always pointing and taking to target path. How to identify the shortcut file path. Ex: Our shortcut file is in desktop and its target file is in 'D' drive [some drive]. When we select the shortcut file from desktop, It always taking to 'D' drive. We need the short cut file path which is in desktop. We are not getting that path. We are able to get the target file path but not able to get the shortcut file path. – Harish M S Sep 20 '20 at 16:27
  • Are you referring to this [FileChooser](https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html)? – Abra Sep 20 '20 at 17:38

1 Answers1

0

Assuming you are using JavaFx FileChooser: FileChooser will return the original .lnk file path if you add .lnk to the file extensions list, otherwise it appears to resolve the pathname as the the link target.

Try this snippet of code to launch FileChooser. Select a LNK file. It will print the original LNK file path even if the link is to a JPG file:

FileChooser fc = new FileChooser();
fc.setTitle("Select a lnk");
fc.setInitialDirectory(dir.toFile());
ObservableList<ExtensionFilter> extensionFilters = fc.getExtensionFilters();
extensionFilters.addAll(new ExtensionFilter("Shortcuts", "*.lnk"), new ExtensionFilter("Images", "*.jpg"));
File f = fc.showOpenDialog(primaryStage);
System.out.println("f="+f);
DuncG
  • 12,137
  • 2
  • 21
  • 33