在 C# 中使用非托管 FindFirstVolume 通过 .NET 枚举卷

发布于 2024-09-28 08:19:12 字数 1751 浏览 3 评论 0原文

我正在尝试枚举没有直接字母安装的驱动器,以便我可以获得每个驱动器上的剩余空间。此应用程序必须在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-10-05 08:19:12

从 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 );

Remove out from before 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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文