问题
It is possible to change the Users Music, Pictures etc Directory. For this, go to your Windows Explorer, Right-Click on "Music" and go to Path.

Here you can see, that I have my Music moved to an other hard disk.
Now the Question: How can I get this Directory in Java?
Because System.getProperty("user.home").concat("\\Music")
leads to "C:\Users\GG\Music" and not to "D:\GG\Music".
UPDATE:
as answered by Anders:
The way that worked for me were the following 3 lines of code:
char[] pszPath = new char[WinDef.MAX_PATH];
Shell32.INSTANCE.SHGetFolderPath(null, ShlObj.CSIDL_MYMUSIC, null, ShlObj.SHGFP_TYPE_CURRENT, pszPath);
File f = new File(String.valueOf(pszPath).trim());
回答1:
The correct way to get the path to special folders on Windows is to call a shell function like SHGetFolderPath with the CSIDL_*
constants (CSIDL_MYMUSIC
in your case).
You need to use JNI or JNA in Java to do this. Examples can be found here and here.
And for completeness, reading from the registry is not the correct way to do this...
回答2:
As pointed out in the discussion beneath, the solution using the registry values isn´t good at all and will most likely not work. You shouldn´t read the registry as it is for storage of those values for windows only. A more programmable interface would be the shell where you can get the same info by typing SHGet*Folder*
. This function is documented here
Thanks to @Anders and @IInspectable who mentioned this.
In the beginning I got this all wrong because the solution I suggested first worked on my PC, so I thought it would be alright. As it turned out this was only a coincidence.
来源:https://stackoverflow.com/questions/44136342/how-do-i-get-the-users-music-directory