Chinaunix首页 | 论坛 | 博客
  • 博客访问: 921817
  • 博文数量: 74
  • 博客积分: 10001
  • 博客等级: 上将
  • 技术积分: 2609
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 19:54
文章存档

2015年(1)

2009年(2)

2008年(71)

我的朋友

分类: WINDOWS

2008-06-03 22:01:47

调用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(" 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(" 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);

阅读(7319) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-17 19:07:26

最近项目中要用到PowerShell,上网找资料,无意中看到了你的博客,对我帮助太大了,谢谢你啊

chinaunix网友2010-04-21 13:54:12

虽然不知道你叫什么,但很欣赏你的博客。 我想请教一下关于PowerShell中调用Win32API 中提到的 Add-Type 命令,为什么我安装的powerShell就没有这个命令呢 我的Add命令中,只有下面这四个。希望能请教你一下,谢谢!我安装的是 Windows PowerShell V2 (CTP) 还有.NetFrameWork3.5 Cmdlet Add-Content Cmdlet Add-History Cmdlet Add-Member Cmdlet Add-PSSnapin