2009-07-03 26 views
47

桌面路径我认为这将在英语语言的Windows安装只工作:如何获得在Java

System.getProperty("user.home") + "/Desktop"; 

我怎样才能使这项工作对非英文Windows?

回答

3

似乎并不那么容易......

但你可以尝试找到一个anwser浏览一些开源项目,例如的代码在Koders。我想所有的解决方案归结为检查Windows注册表中的HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop路径。并且可能是Windows特有的。

如果您需要更通用的解决方案,我会尝试找到一个开源应用程序,您知道该应用程序可以在不同平台上正常工作,并在用户的桌面上放置一些图标。

8

我觉得这是同样的问题...但我不知道!:

In java under Windows, how do I find a redirected Desktop folder?

读它,我希望这个解决方案返回的user.home,但显然不是,答案中的链接评论回来了。还没有尝试过自己。

我想通过使用JFileChooser解决方案将需要一个非无头JVM,但你可能正在运行其中之一。

+4

上面提到的链接给出了正确的答案。 `File home = FileSystemView.getFileSystemView()。getHomeDirectory();`然后如果你需要它作为一个字符串`家。getAbsolutePath();` – 2015-06-28 22:53:48

6

这仅适用于Windows。启动REG.EXE并捕获它的输出:

import java.io.*; 

public class WindowsUtils { 
    private static final String REGQUERY_UTIL = "reg query "; 
    private static final String REGSTR_TOKEN = "REG_SZ"; 
    private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL 
    + "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
    + "Explorer\\Shell Folders\" /v DESKTOP"; 

    private WindowsUtils() {} 

    public static String getCurrentUserDesktopPath() { 
    try { 
     Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD); 
     StreamReader reader = new StreamReader(process.getInputStream()); 

     reader.start(); 
     process.waitFor(); 
     reader.join(); 
     String result = reader.getResult(); 
     int p = result.indexOf(REGSTR_TOKEN); 

     if (p == -1) return null; 
     return result.substring(p + REGSTR_TOKEN.length()).trim(); 
    } 
    catch (Exception e) { 
     return null; 
    } 
    } 

    /** 
    * TEST 
    */ 
    public static void main(String[] args) { 
    System.out.println("Desktop directory : " 
     + getCurrentUserDesktopPath()); 
    } 


    static class StreamReader extends Thread { 
    private InputStream is; 
    private StringWriter sw; 

    StreamReader(InputStream is) { 
     this.is = is; 
     sw = new StringWriter(); 
    } 

    public void run() { 
     try { 
     int c; 
     while ((c = is.read()) != -1) 
      sw.write(c); 
     } 
     catch (IOException e) { ; } 
     } 

    String getResult() { 
     return sw.toString(); 
    } 
    } 
} 

,或者您可以使用JNA(complete example here

Shell32.INSTANCE.SHGetFolderPath(null, 
     ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT, 
     pszPath); 
+1

使用JNA比调用一个进程并解析结果要好得多。 – Sebastian 2014-06-15 15:36:07

-3
public class Sample { 
    public static void main(String[] args) {  
     String desktopPath =System.getProperty("user.home") + "\\"+"Desktop"; 
     String s = "\"" + desktopPath.replace("\\","\\\\") + "\\\\" +"satis" + "\""; 
     System.out.print(s); 
     File f = new File(s); 
     boolean mkdir = f.mkdir(); 
     System.out.println(mkdir); 
    } 
} 
+1

这只会返回英文版本,这不是OP所要的。 – asteri 2012-10-29 11:47:20

+0

不要这样做。如果用户移动了桌面,这将不起作用!如果您有固态C:驱动器,则将桌面移动到其他驱动器上很常见。这会阻止大量写入SSD(读操作不会缩短生命周期,写操作),这意味着您可以拥有一个较小的C:驱动器和一个很大的正常驱动器。 – 2015-06-28 22:36:59

27

我使用Windows的指令,并与它法文版:

System.getProperty("user.home") + "/Desktop"; 

适合我。

+2

不要这样做。如果用户移动了桌面,这将不起作用!如果您有固态C:驱动器,则将桌面移动到其他驱动器上很常见。这会阻止大量写入SSD(读操作不会缩短生命周期,写操作),这意味着您可以拥有一个较小的C:驱动器和一个很大的正常驱动器。 – 2015-06-28 22:36:15

-5

最简单的解决方案是找出机器名称,因为这个名称只是变量在Desktop文件夹路径中的变量。所以如果你能找到这个,你已经找到了桌面的路径。下面的代码应该做的伎俩 - 它为我做:)

String machine_name = InetAddress.getLocalHost().getHostName(); 
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/"; 
-3

有2件事情。

  1. 您正在使用错误的斜杠。对于Windows而言,它是\而不是/
  2. 我使用的是RandomAccesFile和File来管理文件和文件夹,它需要双斜杠(\\)来分隔文件夹名称。
+1

很多java类接受/作为分隔符。这是完全有效的:File f = new File(“S:/folder/file.txt”); – 2015-06-28 22:39:06

-4

您的斜线不正确。你应该像这样使用它。

 try{ 
      String sys = System.getProperty("user.home"); 

      String fileurl = sys+ "\\Desktop\\new"; 
      File newfile = new File(fileurl); 
      newfile.mkdir(); 

      }catch(Exception ioe){ 
      // Handle the error 
       System.out.println("Error"); 
      } 
7
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()