0

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;
}
  • What exactly is FormatDrive doing? As it doesn't seem to have anything connecting it to the device that was created? Also I think if you need to use CreateMountPoint it should be called like so: `CreateMountPoint("C:\mount", deviceID);` where C:\mount is some empty directory you have on an existing filesystem. – thelsdj Aug 15 '11 at 00:26
  • I updated my initial post with the FormatDrive() function. It is supposed to Format the drive after the device is created. Doesn't work at the moment since my device isn't being attached to a Drive Letter yet. –  Aug 15 '11 at 00:29
  • Hmm, that seemed to work for the Mount Point, but my device still doesn't seem to be attached to a letter in My Computer. Is there anything else normally associated with creating a Virtual Disk that I might be missing? –  Aug 15 '11 at 00:32
  • So what happens if you run the code without the CreateMountPoint? What makes you say you have to have that? It seems to me like calling CreateDevice and FormatDrive should be enough. – thelsdj Aug 15 '11 at 00:33
  • Wether I use CreateMountPoint() or not, it doesn't show up in My Computer as a Drive Letter. My console says DeviceID is 5 and free Drive Letter is E, but it doesn't actually appear as a Hard-Drive. The FormatDrive() function doesn't work since the Drive isn't actually appearing. –  Aug 15 '11 at 00:37
  • I would focus on the Format call and try to print out the return value: http://msdn.microsoft.com/en-us/library/aa390432(v=VS.85).aspx – thelsdj Aug 15 '11 at 00:58
  • The Format call I believe is quite irrelevant at the moment. When I an stepping through with the debugger, it should show the Device pop-up as a Drive Letter in My Computer after either CreateDevice() or CreateMountPoint() is called. Unfortunately, it doesn't do so after either, so the Format() will always fail since the Drive Letter isn't there. –  Aug 15 '11 at 01:08
  • Does it show up in Computer Management under Storage -> Disk Management? If it actually shows up as mounted when you run CreateMountPoint you could always hack it and use `subst` to make C:\mount (or whatever path you mount it under) its own drive. Also does CreateDevice return any other useful information to let you know it worked besides setting deviceID? – thelsdj Aug 15 '11 at 01:19

2 Answers2

0

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

Moharrer
  • 137
  • 1
  • 14
0

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!