2

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.

4

2 回答 2

2

首先,“库”不是实际的文件系统位置。库指向文件系统位置。如果您在 win7 中右键单击库并查看其属性,您将看到它指向的实际文件系统位置。默认情况下,文档库指向 C:\Users\{userName}\Documents. 在任何情况下,您都可以使用以下命令访问当前用户的主目录:

System.getProperty("user.home")

因此,要访问文档文件夹:

File documentsFolder = new File(System.getProperty("user.home") + "\\Documents");   
于 2013-05-22T11:09:53.240 回答
0

您可以使用 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));

请参阅获取 Windows 特殊文件夹 (JNA)

作为替代方案,您可以使用 JFileChooser 类提供的方法。

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());
  }
}
于 2013-05-22T11:29:45.273 回答