Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用非托管FindFirstVolume枚举C中带有.NET的卷#_C#_.net_Unmanaged_Winapi - Fatal编程技术网

C# 使用非托管FindFirstVolume枚举C中带有.NET的卷#

C# 使用非托管FindFirstVolume枚举C中带有.NET的卷#,c#,.net,unmanaged,winapi,C#,.net,Unmanaged,Winapi,我试图枚举没有direve字母的驱动器,以便获得每个驱动器上的剩余空间。此应用程序必须与Windows XP配合使用,因此Win32_卷类不可用 执行以下代码时,将引发System.ExecutionEngineeException using System; using System.Text; using System.Runtime.InteropServices; using System.Collections.Generic; class Test : IDisposable {

我试图枚举没有direve字母的驱动器,以便获得每个驱动器上的剩余空间。此应用程序必须与Windows XP配合使用,因此Win32_卷类不可用

执行以下代码时,将引发System.ExecutionEngineeException

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();
    }

}
使用系统;
使用系统文本;
使用System.Runtime.InteropServices;
使用System.Collections.Generic;
类测试:IDisposable
{
公共静态void Main(字符串[]args)
{
尝试
{
GetVolumes();
}
捕获(例外e)
{
Console.WriteLine(如ToString());
}
}
//处理WINAPI FindFirstVolume(
//输出LPTSTR lpszVolumeName,
//以DWORD cchBufferLength表示
//);
[DllImport(“kernel32.dll”,EntryPoint=“FindFirstVolume”,SetLastError=true,CallingConvention=CallingConvention.StdCall)]
公共静态外部内部FindFirstVolume(
out StringBuilder lpszVolumeName,
int cchBufferLength);
[DllImport(“kernel32.dll”,EntryPoint=“FindNextVolume”,SetLastError=true,CallingConvention=CallingConvention.StdCall)]
公共静态外部bool FindNextVolume(
int HFIND卷,
out StringBuilder lpszVolumeName,
int cchBufferLength);
公共静态列表GetVolumes()
{
常数int N=1024;
StringBuilder cVolumeName=新的StringBuilder((int)N);
List ret=新列表();
int volume_handle=FindFirstVolume(out cVolumeName,N);
做
{
ret.Add(cVolumeName.ToString());
Console.WriteLine(cVolumeName.ToString());
}while(FindNextVolume(volume_handle,out cVolumeName,N));
返回ret;
}
void IDisposable.Dispose()无效
{
抛出新的NotImplementedException();
}
}

从StringBuilder之前删除:

[DllImport( "kernel32.dll", EntryPoint = "FindFirstVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern int FindFirstVolume(
  StringBuilder lpszVolumeName,
  int cchBufferLength );


[DllImport( "kernel32.dll", EntryPoint = "FindNextVolume", SetLastError = true, CallingConvention = CallingConvention.StdCall )]
public static extern bool FindNextVolume(
  int hFindVolume,
  StringBuilder lpszVolumeName,
  int cchBufferLength );