在 C# 中使用非托管 FindFirstVolume 通过 .NET 枚举卷
我正在尝试枚举没有直接字母安装的驱动器,以便我可以获得每个驱动器上的剩余空间。此应用程序必须在 Windows XP 上运行,因此 Win32_Volume 类不可用。
当执行以下代码时,会抛出 System.ExecutionEngineException。
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
class Test : IDisposable
{
public static void Main( string[] args )
{
try
{
GetVolumes();
}
catch (Exception e)
{
Console.WriteLine( e.ToString() );
}
}
//HANDLE WINAPI FindFirstVolume(
// __out LPTSTR lpszVolumeName,
// __in DWORD cchBufferLength
//);
[DllImport( "kernel32.dll", EntryPoint = "FindFirstVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern int FindFirstVolume(
out StringBuilder lpszVolumeName,
int cchBufferLength );
[DllImport( "kernel32.dll", EntryPoint = "FindNextVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern bool FindNextVolume(
int hFindVolume,
out StringBuilder lpszVolumeName,
int cchBufferLength );
public static List<string> GetVolumes()
{
const int N = 1024;
StringBuilder cVolumeName = new StringBuilder( (int)N );
List<string> ret = new List<string>();
int volume_handle = FindFirstVolume( out cVolumeName, N );
do
{
ret.Add( cVolumeName.ToString() );
Console.WriteLine( cVolumeName.ToString() );
} while (FindNextVolume( volume_handle, out cVolumeName, N ));
return ret;
}
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
I am trying to enumerate drives that are mounted without a direve letter so I can obtain the remaining space on each of the drives. This application must work with Windows XP so the Win32_Volume class is not available.
When the following code is executed, a System.ExecutionEngineException is thrown.
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
class Test : IDisposable
{
public static void Main( string[] args )
{
try
{
GetVolumes();
}
catch (Exception e)
{
Console.WriteLine( e.ToString() );
}
}
//HANDLE WINAPI FindFirstVolume(
// __out LPTSTR lpszVolumeName,
// __in DWORD cchBufferLength
//);
[DllImport( "kernel32.dll", EntryPoint = "FindFirstVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern int FindFirstVolume(
out StringBuilder lpszVolumeName,
int cchBufferLength );
[DllImport( "kernel32.dll", EntryPoint = "FindNextVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern bool FindNextVolume(
int hFindVolume,
out StringBuilder lpszVolumeName,
int cchBufferLength );
public static List<string> GetVolumes()
{
const int N = 1024;
StringBuilder cVolumeName = new StringBuilder( (int)N );
List<string> ret = new List<string>();
int volume_handle = FindFirstVolume( out cVolumeName, N );
do
{
ret.Add( cVolumeName.ToString() );
Console.WriteLine( cVolumeName.ToString() );
} while (FindNextVolume( volume_handle, out cVolumeName, N ));
return ret;
}
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 StringBuilder 之前删除:
Remove out from before StringBuilder: