如何使用 Cocoa 查找已安装卷上的可用空间量?
我使用以下代码来确定卷上的可用空间。 该文件夹是使用 NSOpenPanel 提供的。 所选项目是已安装卷,返回的路径是 \Volumes\Name
NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];
unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];
有没有更好的方法来使用 Cocoa 确定已安装卷上的可用空间?
更新:这实际上是确定卷上可用空间的最佳方法。 看起来它不起作用,但这是因为该文件夹实际上是 /Volumes 而不是 /Volume/VolumeName
I am using the following code to determine free space on a volume.
The folder was provided using NSOpenPanel. The item selected was a mounted volume and the path returned is \Volumes\Name
NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];
unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];
Is there a better method to determine the free space on a mounted volume using Cocoa?
Update: This is in fact the best way to determine the free space on a volume. It appeared it wasn't working but that was due to the fact that folder was actually /Volumes rather than /Volume/VolumeName
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
statfs 与 df 的结果一致。 理论上 NSFileSystemFreeSize 来自 statfs,所以你的问题不应该存在。
您可能需要按如下方式运行 statfs 作为 NSFileSystemFreeSize 的替代品:
statfs is consistent with results from df. In theory NSFileSystemFreeSize comes from statfs, so your problem should not exist.
You may want to run statfs as below as a replacement for NSFileSystemFreeSize:
提供的代码是 Cocoa 中确定卷上可用空间的最佳方法。
只需确保提供给 [NSFileManagerObj fileSystemAttributesAtPath] 的路径包含卷的完整路径。 我删除了最后一个路径组件,以确保传递的是文件夹而不是文件,这导致 /Volumes 被用作文件夹,而无法给出正确的结果。
The code provided IS the best way in Cocoa to determine the free space on a volume.
Just make sure that the path provided to [NSFileManagerObj fileSystemAttributesAtPath] includes the full path of the volume. I was deleting the last path component to assure that a folder rather than a file was passed in which resulted in /Volumes being used as the folder which does not give the right results.