Question

In the company, we've got several global Java applications used by our employees. Some of them allow the users to create reports, which are always saved in their home directory. The home directories are network drives and are assigned the drive letter U:/ when a user logs himself in on his computer. So, in the applications, the path to the report destination directory is simply a hard coded U:\Reports.

However, we will soon migrate from Windows XP to Windows 7 and use a different structure: The users will get new home directories on other servers, which will be accessible in the Documents Library in the Windows 7 Explorer. There wont be any drive letters anymore.

So the new path for the report directory is supposed to be Libraries\Documents\My Documents\Reports. But how would I be able to access this path in Java? How can I find the actual, absolute UNC path (if this is even necessary)?

I can't just use \\theserver\users\username, since we've got multiple servers (one for every continent). I have to use the folder in the Windows 7 Document library.

Was it helpful?

Solution

Firstly, "Libraries" is not an actual file-system location. Libraries point to file-system locations. If you right-click on a library in win7 and view its properties, you'll see the actual file-system location(s) it points to. By default, the Documents library points to C:\Users\{userName}\Documents. In any case, you can access the current user's home directory by using:

System.getProperty("user.home")

So, to access the documents folder:

File documentsFolder = new File(System.getProperty("user.home") + "\\Documents");   

OTHER TIPS

You can use JNA :

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.ShlObj;
import com.sun.jna.platform.win32.WinDef;

...

    char[] pszPath = new char[WinDef.MAX_PATH];
    Shell32.INSTANCE.SHGetFolderPath(null,
      ShlObj.CSIDL_MYDOCUMENTS, null, ShlObj.SHGFP_TYPE_CURRENT,
      pszPath);
    System.out.println(Native.toString(pszPath));

See Get Windows Special Folders (JNA)

As an alternative, you can use a method provided by the JFileChooser class.

import javax.swing.JFileChooser;
javax.swing.filechooser.FileSystemView;

public class GetMyDocuments {
  public static void main(String args[]) {
     JFileChooser fr = new JFileChooser();
     FileSystemView fw = fr.getFileSystemView();
     System.out.println(fw.getDefaultDirectory());
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top