是否可以在不知道确切位置的情况下在 Mac 上找到文件?
我试图在 Machintosh 上查找文件,但不知道确切的文件位置。我正在开发一个应用程序,需要找到 iTunes 库 XML 文件并解析它的数据。然而XML驻留在其中,
/Users/**Current logged in username**/Music/iTunes
现在用户名可能会改变,那么如何设置让我的代码以编程方式找到下面命名文件的路径。使用的语言是 Objective-C。
File name : iTunes Music Library.xml
我认为可能的解决方案是通过代码获取用户名。是否有任何方法返回当前登录用户的“用户名”字符串。
谢谢
I am trying to locate a file on Machintosh without knowing exact file location. I am developing an app which needs to locate iTunes library XML file and parse it for data. However XML resides in,
/Users/**Current logged in username**/Music/iTunes
Now username may change, So how to set path of let my code find below named file programmatically. Language used is Objective - C.
File name : iTunes Music Library.xml
Possible solution I thought is to get username by code. Is there any method which returns a string of current logged in user's "username".
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用
~/Music/iTunes
。~
代表当前用户的主目录。或者您可以看到这个问题: Mac OS X:从目录服务获取当前用户的当前用户名和主目录
You should be able to use
~/Music/iTunes
.~
represents the home directory of the current user.Or you could could see this question: Mac OS X: Get current username and home directory for current user from Directory Services
不要这样做!用户的 iTunes 资料库不必位于默认位置。
相反,从 com.apple.iapps 读取 iTunesRecentDatabasePaths 键。它会给你一个代表最近使用的 iTunes 库的数组。
Don't do this! The user's iTunes library doesn't have to be in the default location.
Instead, read the iTunesRecentDatabasePaths key from com.apple.iapps. It'll give you an array that represents the recently used iTunes libraries.
NSHomeDirectory() 是一个 Foundation 函数,它返回当前用户主目录路径的 NSString。
Apple 开发者库 - NSHomeDirectory
NSHomeDirectory() is a Foundation function that returns an NSString of the path to the current user's home directory.
Apple Developer Library - NSHomeDirectory