1

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.

German Screenshot

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());
  • A bit outdated, but I think those are still valid answers... [https://stackoverflow.com/q/2896610/1759845](https://stackoverflow.com/q/2896610/1759845) – BackSlash May 23 '17 at 13:36
  • I don't know which registry setting it is, but it'll be one of them: https://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java – slim May 23 '17 at 13:37

2 Answers2

2

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...

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Bonus points for the Raymond Chen link. The current link is dead (a-specific redirect to Raymonds index-page, not the specific article). Current link is: [The long and sad story of the Shell Folders key](https://devblogs.microsoft.com/oldnewthing/20031103-00/?p=41973) – Jules Kerssemakers Feb 21 '23 at 13:25
  • @JulesKerssemakers Yes, his blog moves every 3 years or so and MS are unable to keep simple things working. Linking to the original URL on Archive.org is better because it retains the comments. – Anders Feb 21 '23 at 15:08
1

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.

creyD
  • 1,972
  • 3
  • 26
  • 55
  • 1
    Just looking for random keys in the registry is not the best approach; https://blogs.msdn.microsoft.com/oldnewthing/20031103-00/?p=41973 – Anders May 23 '17 at 14:29
  • How are they random? I searched my computer where the information is stored and wrote it down here. Just because your article says that the keys only exist because of some programs doesn´t mean my answer is wrong. If you think you have a better approach I invite you to join the discussion. – creyD May 23 '17 at 15:04
  • Exactly, you searched and assumed. There is another shell key where the real information is stored but you should not be using that either. The only documented way is to call one of the `SHGet*Folder*` functions, it says so right in the blog post written by somebody who works on the Windows shell. – Anders May 23 '17 at 15:59
  • Ok I see (I didn´t read the whole article before). But if he just wants to find out where the music folder is he could take the registry value I mentioned aswell, am I right? BTW May I integrate your posts into my answer? – creyD May 23 '17 at 16:06
  • 1
    The music path might not be in that key, the "user shell folders" key is better but there is no excuse not to call the correct function once it has been pointed out to you. – Anders May 23 '17 at 16:30
  • @Anders I asked if I could integrate your comment into my answer? "might not be in that key..." --> But it is. That´s what I´ve looked up in the beginning. – creyD May 23 '17 at 16:43
  • The registry is not a public programming interface. The real solution is to ask the Shell. – IInspectable May 23 '17 at 17:08
  • @IInspectable Now I understand the whole point. Thank you guys. I will include this into my answer now. – creyD May 23 '17 at 17:09
  • @creyD again, the registry value might be there but only if somebody else asked for it with the API. – Anders May 23 '17 at 17:42
  • @Anders I edited the answer to adjust your point. Is it better now? – creyD May 23 '17 at 17:50
  • You should link to one of the functions on MSDN, https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx for example – Anders May 23 '17 at 17:51
  • @Anders: The Old-New-thing link was broken by microsoft moving their main blog-URL, with only crappy, generic redirects. Based on the date-stamp of your old link, the current link is: [The long and sad story of the Shell Folders key](https://devblogs.microsoft.com/oldnewthing/20031103-00/?p=41973) – Jules Kerssemakers Feb 21 '23 at 13:28