3

对于一个项目,我想获取 NTFS 分区上所有可用/已使用集群的列表。为此,我必须转储 $Bitmap 文件并解析其内容。

网络上的 API 和示例很少,但它们似乎不起作用。是否有一种简单的方法/代码示例可以将 $Bitmap 文件复制到某处。

使用 FSCTL_GET_VOLUME_BITMAP 是唯一的方法吗?理想情况下,我想用 C# 来做。

4

2 回答 2

1

NFI.EXE 是(曾经是)“oem 支持工具”的一部分,可以枚举所有 NTFS 分区项。它也可能能够转储 $Bitmap 的内容。

在此处输入图像描述

于 2012-04-26T07:47:50.660 回答
1

您肯定想走简单的路线并使用 IOCTL,而不是尝试$Bitmap直接阅读。当然,如果有人为你做了,你不必自己做。事实证明,一位 MSDN 博主已经为您编写了一个不错的小包装器:

http://blogs.msdn.com/b/jeffrey_wall/archive/2004/09/13/229137.aspx

整个类300多行代码,就不一一贴出来了,下面是获取音量位图的函数:

    /// <summary>
    /// Get cluster usage for a device
    /// </summary>
    /// <param name="DeviceName">use "c:"</param>
    /// <returns>a bitarray for each cluster</returns>
    static public BitArray GetVolumeMap(string DeviceName)
    {
        IntPtr pAlloc = IntPtr.Zero;
        IntPtr hDevice = IntPtr.Zero;

        try
        {
            hDevice = OpenVolume(DeviceName);

            Int64 i64 = 0;

            GCHandle handle = GCHandle.Alloc(i64, GCHandleType.Pinned);
            IntPtr p = handle.AddrOfPinnedObject();

            // alloc off more than enough for my machine
            // 64 megs == 67108864 bytes == 536870912 bits == cluster count
            // NTFS 4k clusters == 2147483648 k of storage == 2097152 megs == 2048 gig disk storage
            uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 == 1 meg * 64 == 64 megs

            uint size = 0;
            pAlloc = Marshal.AllocHGlobal((int)q);
            IntPtr pDest = pAlloc;

            bool fResult = DeviceIoControl(
                hDevice,
                FSConstants.FSCTL_GET_VOLUME_BITMAP,
                p,
                (uint)Marshal.SizeOf(i64),
                pDest,
                q,
                ref size,
                IntPtr.Zero);

            if (!fResult)
            {
                throw new Exception(Marshal.GetLastWin32Error().ToString());
            }
            handle.Free();

            /*
            object returned was...
      typedef struct 
      {
       LARGE_INTEGER StartingLcn;
       LARGE_INTEGER BitmapSize;
       BYTE Buffer[1];
      } VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
            */
            Int64 StartingLcn = (Int64)Marshal.PtrToStructure(pDest, typeof(Int64));

            Debug.Assert(StartingLcn == 0);

            pDest = (IntPtr)((Int64)pDest + 8);
            Int64 BitmapSize = (Int64)Marshal.PtrToStructure(pDest, typeof(Int64));

            Int32 byteSize = (int)(BitmapSize / 8);
            byteSize++; // round up - even with no remainder

            IntPtr BitmapBegin = (IntPtr)((Int64)pDest + 8);

            byte[] byteArr = new byte[byteSize];

            Marshal.Copy(BitmapBegin, byteArr, 0, (Int32)byteSize);

            BitArray retVal = new BitArray(byteArr);
            retVal.Length = (int)BitmapSize; // truncate to exact cluster count
            return retVal;
        }
        finally
        {
            CloseHandle(hDevice);
            hDevice = IntPtr.Zero;

            Marshal.FreeHGlobal(pAlloc);
            pAlloc = IntPtr.Zero;
        }
    }
于 2012-04-25T06:28:25.680 回答