如何在Windows中获取驱动器的总大小 [英] How to get total size of a drive in Windows

查看:53
本文介绍了如何在Windows中获取驱动器的总大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用标准的Windows api调用在Windows上的Go中获取驱动器的总大小;

I want to get total size of a drive in Go on windows using standard windows api call;

我发现以获取可用空间.

I found this to get the free space.

现在我想例如使用特殊驱动器的总空间大小

Now I want to total space size of special drive for example

C:\

推荐答案

您所链接的问题+答案显示了如何获得可用空间.该解决方案使用 GetDiskFreeSpaceExW() Windows API函数从 kernel32.dll 进行获取.

Your linked question+answer shows how to get the free space. The solution uses the GetDiskFreeSpaceExW() windows API function from the kernel32.dll to obtain it.

相同的函数也可以用于获取总大小. GetDiskFreeSpaceExW()函数的签名:

The same function can be used to get total size too. Signature of the GetDiskFreeSpaceExW() function:

BOOL GetDiskFreeSpaceExW(
  LPCWSTR         lpDirectoryName,
  PULARGE_INTEGER lpFreeBytesAvailableToCaller,
  PULARGE_INTEGER lpTotalNumberOfBytes,
  PULARGE_INTEGER lpTotalNumberOfFreeBytes
);

它具有输入参数,路径,并且具有3个输出参数,即空闲字节(可供调用者使用),总字节(磁盘大小)和总空闲字节.

It has an in-parameter, the path, and it has 3 out-parameters, namely the free bytes (available to caller), the total bytes (disk size) and the total free bytes.

因此,只需在调用它时,为要获取的所有信息提供变量(指针)即可.

So simply when you call it, provide variables (pointers) for all info you want to get out of it.

例如:

kernelDLL := syscall.MustLoadDLL("kernel32.dll")
GetDiskFreeSpaceExW := kernelDLL.MustFindProc("GetDiskFreeSpaceExW")

var free, total, avail int64

path := "c:\\"
r1, r2, lastErr := GetDiskFreeSpaceExW.Call(
    uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
    uintptr(unsafe.Pointer(&free)),
    uintptr(unsafe.Pointer(&total)),
    uintptr(unsafe.Pointer(&avail)),
)

fmt.Println(r1, r2, lastErr)
fmt.Println("Free:", free, "Total:", total, "Available:", avail)

运行它,输出示例:

1 0 Success.
Free: 16795295744 Total: 145545281536 Available: 16795295744

这篇关于如何在Windows中获取驱动器的总大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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