Question

I have used the ImDisk library with the .NET wrapper to create a Virtual Disk in my C# application. However, after I create the device, I apparently need to create a Mount Point as well for the device to actually show as a Drive Letter. I don't completely understand what is supposed to be supplied for it to create a Mount Point, but I believe this pertains more to Virtual devices than the library.

My Function:

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 Definition:

        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

UPDATE

FormatDrive Function:

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;
}
Était-ce utile?

La solution

Turns out their were some issues with the parameters being passed in CreateDevice(), which was allowing it to not generate errors but not fully completing the setup process.

Thanks for the help!

Autres conseils

you must add ":" at the end of driveLetter parameter

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top