4

Just wondering if there is a way to return back the UNC path from a file chosen with JFileChooser. The file that I would be selecting would reside on a mapped drive that has a UNC path. Right now, I can only seem to pull back the drive letter of a mapped drive.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
tasoocoo
  • 41
  • 1
  • 3
  • The `JFileChooser` method `getSelectedFile()` returns a `File` which has this information. – trashgod Apr 19 '11 at 21:12
  • this helped me out quite a bit, although i needed the UNC path of the current machine, rather than a mapped drive. using 'ipconfig /all' and slicing it up should do the trick. you should take that edit above and post it as an answer to get cred. thanks for posting the solution. – moonlightcheese Mar 08 '12 at 21:38
  • @trashgod: there's some reputation waiting for you here ;P. – Zecas May 28 '12 at 09:05
  • @moonlightcheese: Sorry I overlooked this earlier; thanks to Zecas for the heads-up. – trashgod May 28 '12 at 12:34

4 Answers4

3

From https://stackoverflow.com/users/715934/tasoocoo

I ended up finding a solution that executes the NET USE command:

 filePath = fc.getSelectedFile().getAbsolutePath();
 Runtime runTime = Runtime.getRuntime();
 Process process = runTime.exec("net use");
 InputStream inStream = process.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 String line = null;
 String[] components = null;
 while (null != (line = bufferedReader.readLine())) {
   components = line.split("\\s+");
    if ((components.length > 2) && (components[1].equals(filePath.substring(0, 2)))) {
      filePath = filePath.replace(components[1], components[2]);
    }
 }
Community
  • 1
  • 1
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
1

The JFileChooser method getSelectedFile() returns a File, which may have helpful information.

"For Microsoft Windows platforms,…the prefix of a UNC pathname is "\\\\"; the hostname and the share name are the first two names in the name sequence."

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

As I commented on Gerry's answer, ShellFolder.getDisplayName is unreliable because the user can changed the display name to whatever they want.

However the UNC path does seem to be available via sun.awt.shell.ShellFolder. This is of course an "internal proprietary API" so no guarantee this will continue to work in future versions of java, but testing against java 1.8.0_31 in Windows 7 I see a ShellFolderColumnInfo titled Attributes which for network drives appears to include the UNC path as a bare String. eg:

File networkDrive = new File("G:\");
ShellFolder shellFolder = ShellFolder.getShellFolder(networkDrive);
ShellFolderColumnInfo[] cols = shellFolder.getFolderColumns();
for (int i = 0; i < cols.length; i++) {
    if ("Attributes".equals(cols[i].getTitle())) {
        String uncPath = (String) shellFolder.getFolderColumnValue(i);
        System.err.println(uncPath);
        break; // don't need to look at other columns
    }
}

If you go to My Computer in explorer, change to Details view and turn on the "Network Location" column, it appears to match what we get from "Attributes" via the ShellFolder API. Not sure where "Attributes" comes from or if it changes in non-english locales.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
sqweek
  • 1,129
  • 9
  • 12
  • Ok the JVM seems to use `IShellDetails::getDetailsOf` to populate `ShellFolderColumnInfo.title` which as stated here can change according to locale: https://msdn.microsoft.com/en-us/library/windows/desktop/bb775104%28v=vs.85%29.aspx But msdn also says columns 0/1/2/3 are always Name/Size/Type/Date Modified. Java's ColumnInfo titles match MSDN, but in reality the column values represent DiskLabel/Type/Size/FreeSpace. It may be more portable to directly index the column, ie `String uncPath = (String) shellFolder.getFolderColumnValue(6);` – sqweek Aug 09 '16 at 05:38
  • Also note that the Size column (1) which really store type could possibly be used I found a local drive reported 'Local Disk' and a network drive reported 'Network Drive' – Paul Taylor Sep 08 '17 at 08:43
0

If anyone else is looking for an alternate (and I think simpler) solution, you can find the information using ShellFolder.getDisplayName(). For example, you can parse the network location of the drive from the string here:

System.out.println(ShellFolder.getShellFolder(new File(filePath.substring(0,3))).getDisplayName());

This might also be useful:

File.listRoots();
Gerry
  • 53
  • 6
  • 3
    This works because windows by default sets the drive label to something like `$SHARENAME (\\$SERVER)` for mapped network drives. However the user is free to change this label to something completely different, so it's unwise to rely on `ShellFolder.getDisplayName` always having this information. – sqweek Aug 09 '16 at 04:10