创建虚拟磁盘挂载点

时间:2011-08-15 00:16:59

标签: c# .net ramdisk mount-point virtual-disk

我已经使用ImDisk库和.NET包装器在我的C#应用​​程序中创建了一个虚拟磁盘。但是,在我创建设备之后,我显然需要创建一个Mount Point,以便设备实际显示为Drive Letter。我不完全理解为它创建一个挂载点应该提供什么,但我相信这与虚拟设备有关而不是库。

我的功能:

public bool CreateRAMDisk()
{
    // Create Empty RAM Disk
    char driveLetter = ImDiskAPI.FindFreeDriveLetter();

    ImDiskAPI.CreateDevice(52428800, 0, 0, 0, 0, ImDiskFlags.DeviceTypeHD | ImDiskFlags.TypeVM, null, false, driveLetter.ToString(), ref deviceID, IntPtr.Zero);

    string mountPoint = driveLetter + @":\Device\ImDisk0";
    ImDiskAPI.CreateMountPoint(mountPoint, deviceID);

    // Format the Drive for NTFS
    if (FormatDrive(driveLetter.ToString(), "NTFS", true, 4096, "", false))
    {

CreateMountPoint定义:

        public static void CreateMountPoint(string Directory, uint DeviceNumber);
        //
        // Summary:
        //     Creates a mount point for an ImDisk virtual disk on an empty subdirectory
        //     on an NTFS volume.
        //
        // Parameters:
        //   Directory:
        //     Path to an empty subdirectory on an NTFS volume
        //
        //   DeviceNumber:
        //     Device number of an existing ImDisk virtual disk

更新

FormatDrive功能:

public static bool FormatDrive(string driveLetter, string fileSystem, bool quickFormat, int clusterSize, string label, bool enableCompression)
{
    driveLetter = driveLetter + ":";

    if (driveLetter.Length != 2 || driveLetter[1] != ':'|| !char.IsLetter(driveLetter[0]))
    {
        return false;
    }

    //query and format given drive         
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");

    foreach (ManagementObject vi in searcher.Get())
    {
        vi.InvokeMethod( "Format", new object[] {fileSystem, quickFormat, clusterSize, label, enableCompression} );
    }

    return true;
}

2 个答案:

答案 0 :(得分:0)

原来他们在CreateDevice()中传递的参数存在一些问题,它允许它不会生成错误但不能完全完成设置过程。

感谢您的帮助!

答案 1 :(得分:0)

您必须在driveLetter参数的末尾添加“:”