完全重复: Getting the size (free,total) of a Windows Mobile phone drive using c#
<小时/> 亲爱的;
我知道我的问题花了很多时间,很多你帮助了我,但我是C#的新手,这是我的第一个应用程序..
现在我读了一篇文章:
C#签名:
[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
示例代码:
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out
TotalNumberOfBytes,out TotalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
Console.WriteLine("Free Bytes Available: {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes: {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
现在如何使用此函数GetDiskFreeSpaceEx,我应该将C#签名添加到某个地方?!?那coredll.dll怎么样?!?
我的代码是这样的:
FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer,
typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0)
{
// here i want to get the Total Size of the currentDirectory and freeSize
// i want them in Bytes
}
我在谷歌搜索但我没有足够的经验知道正确的标签
日Thnx
答案 0 :(得分:2)
基本上,你可以像任何其他静态方法一样调用它,就像这样:
FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0)
{
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
}
我建议创建一个包装器来处理抛出异常(除非你想每次检查返回值而不处理异常):
[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto, EntryPoint="GetDiskFreeSpaceEx")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool InternalGetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);
static GetDiskFreeSpaceEx(string directoryName, out ulong freeBytesAvailable, out ulong totalNumberOfBytes, out totalNumberOfFreeBytes);
{
if (!GetDiskFreeSpaceEx(directoryName, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes))
throw new System.ComponentModel.Win32Exception();
}
呼叫网站将成为:
FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0)
{
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);
}
答案 1 :(得分:0)
您的原始代码确实令人困惑,您可以使用上一个问题original question here中链接的原始代码。事实上,你不应该首先打开另一个问题,但应该在原文中添加评论,要求进一步澄清。
您有P / Invoke定义。这只是一个静态方法调用。你把它放在课堂上。像这样:
public class MyClass
{
[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
}
然后你打电话给它(不知道你想要用你所有的编组工作做什么):
ulong GetDiskSize(string volumeName)
{
ulong avail;
ulong total;
ulong totalfree;
MyClass.GetDiskFreeSpaceEx(volumeName, out avail, out total, out totalFree);
return total;
// return others as desired
}
然后使用它就像这样:
ulong diskSize = GetDiskSize("\\Storage Card");