2009-07-03 67 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()