3
J'essaie d'énumérer les lecteurs qui sont montés sans une lettre direve afin que je puisse obtenir l'espace restant sur chacun des lecteurs. Cette application doit fonctionner avec Windows XP afin que la classe Win32_Volume ne soit pas disponible.Utilisez FindFirstVolume non managé pour énumérer des volumes avec .NET en C#
Lorsque le code suivant est exécuté, une exception System.ExecutionEngineException est levée.
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();
}
}