如何获取用户音乐目录? [英] How do I get the Users Music Directory?

查看:49
本文介绍了如何获取用户音乐目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以更改用户音乐、图片等目录.为此,请转到您的 Windows 资源管理器,右键单击音乐"并转到路径.

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.

现在的问题是:如何在 Java 中获取此目录?

Now the Question: How can I get this Directory in Java?

因为 System.getProperty("user.home").concat("\\Music") 导致 "C:\Users\GG\Music" 而不是 "D:\GG\音乐".

Because System.getProperty("user.home").concat("\\Music") leads to "C:\Users\GG\Music" and not to "D:\GG\Music".

更新:

安德斯的回答:

对我有用的方式是以下 3 行代码:

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());

推荐答案

在 Windows 上获取特殊文件夹路径的正确方法是调用 shell 函数,如 SHGetFolderPathCSIDL_*code> 常量(在您的情况下为 CSIDL_MYMUSIC).

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

您需要在 Java 中使用 JNI 或 JNA 来执行此操作.示例可以在此处此处.

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

这篇关于如何获取用户音乐目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆