Windows Mobile获取存储卡容量及使用情况

本文介绍了一种在WindowsMobile设备上获取存储卡空间信息的方法,通过C#封装API实现对存储卡总空间、可用空间等信息的读取,并提供了实际运行测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做Windows Mobile开发的时候,为了节省空间,很多文件要放到存储卡上,因而对存储卡空间及容量的管理就极为重要。我们可以通过封装GetDiskFreeSpaceEx API来完成该功能,具体C#代码如下。

public static DiskFreeSpace GetDiskFreeSpace(string directoryName)
{
DiskFreeSpace result = new DiskFreeSpace();

if (!GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable, ref result.TotalBytes, ref result.TotalFreeBytes))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error retrieving free disk space");
}
return result;
}

public struct DiskFreeSpace
{
public long FreeBytesAvailable;

public long TotalBytes;

public long TotalFreeBytes;
}

[DllImport("coredll")]
private static extern bool GetDiskFreeSpaceEx(string directoryName,
ref long freeBytesAvailable,
ref long totalBytes,
ref long totalFreeBytes);

下面代码用来测试,本人在Windows Mobile5.0 及Windows Mobile6.0的Pocket PC上测试通过。

private void button1_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
DiskFreeSpace dis = GetDiskFreeSpace("Storage Card");
MessageBox.Show("总空间:"+dis.TotalBytes.ToString()+"/r/n剩余空间:"+dis.TotalFreeBytes.ToString()+"/r/n");
}
catch (Exception ep)
{
MessageBox.Show(ep.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值