[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] PowerShell中调用Win32API

调用Win32 API, 取得网络文件夹的剩余空间. 用户无需挂载磁盘即可取得结果

$a = Add-Type -memberDefinition @"
[DllImport("Kernel32.dll")]
public static extern bool GetDiskFreeSpaceEx(
string lpDirectoryName,
out long lpFreeBytesAvailable,
out long lpTotalNumberOfBytes,
out long lpTotalNumberOfFreeBytes
);
"@ -passthru -name MyGetDiskFreeSpaceEx

$fba = [int64] 0;
$tnb = [int64] 0;
$nfb = [int64] 0;
$a::GetDiskFreeSpaceEx("\\.host\Shared Folders\files", [ref] $fba, [ref] $tnb, [ref] $nfb)
"FreeBytesAvailable: $($x)"
"TotalNumberOfBytes: $($y)"
"TotalNumberOfFreeBytes: $($z)"

------------OLD------------

$a = Add-Type -memberDefinition @"
[DllImport("Kernel32.dll")]
public static extern bool GetDiskFreeSpaceEx(
string lpDirectoryName,
IntPtr lpFreeBytesAvailable,
IntPtr lpTotalNumberOfBytes,
IntPtr lpTotalNumberOfFreeBytes
);
"@ -passthru -name MyGetDiskFreeSpaceEx

$fba = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(20);
$tnb = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(20);
$nfb = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(20);


$a::GetDiskFreeSpaceEx("\\.host\Shared Folders\files", $fba, $tnb,  $nfb)


$x = [System.Runtime.InteropServices.Marshal]::ReadInt64($fba)
$y = [System.Runtime.InteropServices.Marshal]::ReadInt64($tnb)
$z = [System.Runtime.InteropServices.Marshal]::ReadInt64($nfb)

"FreeBytesAvailable: $($x)"
"TotalNumberOfBytes: $($y)"
"TotalNumberOfFreeBytes: $($z)"

[System.Runtime.InteropServices.Marshal]::FreeHGlobal($fba);
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($tnb);
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($nfb);

http://blog.chinaunix.net/uid-9781829-id-1997931.html

返回列表