获取可用磁盘空间
鉴于下面的每个输入,我想在该位置获得可用空间。像输入这样的东西
long GetFreeSpace(string path)
:
c:
c:\
c:\temp
\\server
\\server\C\storage
Given each of the inputs below, I'd like to get free space on that location. Something like
long GetFreeSpace(string path)
Inputs:
c:
c:\
c:\temp
\\server
\\server\C\storage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这对我有用......
this works for me...
使用来自 RichardOD 链接的
GetDiskFreeSpaceEx
的工作代码片段。Working code snippet using
GetDiskFreeSpaceEx
from link by RichardOD.DriveInfo 将帮助您解决其中一些问题(但它不适用于 UNC 路径),但实际上我认为您需要使用 GetDiskFreeSpaceEx。您或许可以使用 WMI 实现某些功能。 GetDiskFreeSpaceEx 看起来是您最好的选择。
您可能必须清理路径才能使其正常工作。
DriveInfo will help you with some of those (but it doesn't work with UNC paths), but really I think you will need to use GetDiskFreeSpaceEx. You can probably achieve some functionality with WMI. GetDiskFreeSpaceEx looks like your best bet.
Chances are you will probably have to clean up your paths to get it to work properly.
这是 @sasha_gud 答案的重构和简化版本:
Here is a refactored and simplified version of the @sasha_gud answer:
我正在寻找以 GB 为单位的大小,因此我只是改进了上面 Superman 的代码,进行了以下更改:
I was looking for the size in GB, so I just improved the code from Superman above with the following changes:
看看这个(这对我来说是一个可行的解决方案)
Check this out (this is a working solution for me)
未经测试:
顺便说一句,c:\temp 上的可用磁盘空间的结果是什么?您将获得 c:\ 的空闲空间
untested:
Btw what is the outcome of free diskspace on c:\temp ? you will get the space free of c:\
你可以试试这个:
祝你好运
You can try this:
Good luck
请参阅此文章!
通过搜索“:”索引识别UNC par或本地驱动器路径
通过
如果它是UNC PATH,您可以映射UNC路径
执行驱动器名称的代码被映射的驱动器名称
UNC 映射驱动器或本地驱动器>。
完成要求后取消映射。
see this article!
identify UNC par or local drive path by searching index of ":"
if its is UNC PATH you cam map UNC path
code to execute drive name is mapped drive name < UNC Mapped Drive or Local Drive>.
unmap after you requirement done.
作为这个回答和@RichardOD建议,你应该这样做:
As this answer and @RichardOD suggested , you should do like this:
我想要为我的项目使用类似的方法,但在我的例子中,输入路径要么来自本地磁盘卷,要么来自集群存储卷 (CSV)。所以 DriveInfo 类对我不起作用。 CSV 在另一个驱动器下有一个装载点,通常是 C:\ClusterStorage\Volume*。请注意,C: 将是与 C:\ClusterStorage\Volume1 不同的卷
这是我最终想出的:
I wanted a similar method for my project but in my case the input paths were either from local disk volumes or clustered storage volumes (CSVs). So DriveInfo class did not work for me. CSVs have a mount point under another drive, typically C:\ClusterStorage\Volume*. Note that C: will be a different Volume than C:\ClusterStorage\Volume1
This is what I finally came up with:
我遇到了同样的问题,我看到 waruna manjula 给出了最佳答案。
然而,将所有内容写在控制台上可能并不是您想要的。
要从所有信息中获取字符串,请使用以下
步骤第一步:在开始处声明值
第二步:实际代码
我想指出的是,您可以将所有控制台写入行注释代码,但我认为您测试一下它会很好。
如果您依次显示所有这些,您将获得与 waruna majuna
Drive C:\ 相同的列表
驱动类型:固定式
卷标:
文件系统:NTFS
当前用户可用空间:134880153600 字节
总可用空间:134880153600 字节
驱动器总大小:499554185216 字节
驱动器 D:\
驱动器类型:CDROM
驱动器 H:\
驱动类型:固定式
卷标:HDD
文件系统:NTFS
当前用户可用空间:2000010817536 字节
总可用空间:2000010817536 字节
驱动器总大小:2000263573504 字节
但是您现在可以通过字符串访问所有松散信息
I had the same problem and i saw waruna manjula giving the best answer.
However writing it all down on the console is not what you might want.
To get string off al info use the following
Step one: declare values at begin
Step 2: actual code
I want to note that you can just make all the console writelines comment code, but i thought it would be nice for you to test it.
If you display all these after each other you get the same list as waruna majuna
Drive C:\
Drive type: Fixed
Volume label:
File system: NTFS
Available space to current user: 134880153600 bytes
Total available space: 134880153600 bytes
Total size of drive: 499554185216 bytes
Drive D:\
Drive type: CDRom
Drive H:\
Drive type: Fixed
Volume label: HDD
File system: NTFS
Available space to current user: 2000010817536 bytes
Total available space: 2000010817536 bytes
Total size of drive: 2000263573504 bytes
However you can now acces all of the loose information at strings