How can I create a RAM disk programmatically?

WinFsp allows you to define your own file system programmatically, using plain c++ or also dotNet C#. The example project is a file system persisted in OS RAM memory. You may also take a look at Dokany.

https://github.com/billziss-gh/winfsp

https://github.com/dokan-dev/dokany


you didnt mentioned the language , sO my answer is in c# :

A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory. Starting with the .NET Framework version 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files in Win32 in the MSDN Library.

http://msdn.microsoft.com/en-us/library/dd997372.aspx

MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen(
       new FileStream(@"C:\temp\Map.mp", FileMode.Create), // Any stream will do it      
       "MyMemMapFile",                                     // Name
       1024 * 1024,                                        // Size in bytes
       MemoryMappedFileAccess.ReadWrite);                  // Access type

// Create the map view and read it:

using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
       Container NewContainer = new Container();
       FileMap.Read<Container>(4, out NewContainer);
}

ImDisk is a RAM disk app that creates a virtual drive from a sector of memory, and has an API that can be called from .NET.

class RamDisk
{
    public const string MountPoint = "X:";

    public void createRamDisk()
    {

        try
        {
            string initializeDisk   = "imdisk -a ";
            string imdiskSize       = "-s 1024M ";
            string mountPoint       = "-m "+ MountPoint + " ";


            ProcessStartInfo procStartInfo  = new ProcessStartInfo();
            procStartInfo.UseShellExecute   = false;
            procStartInfo.CreateNoWindow    = true;
            procStartInfo.FileName          = "cmd";
            procStartInfo.Arguments         = "/C " + initializeDisk + imdiskSize + mountPoint;
            Process.Start(procStartInfo);

            formatRAMDisk();

        }
        catch (Exception objException)
        {
            Console.WriteLine("There was an Error, while trying to create a ramdisk! Do you have imdisk installed?");
            Console.WriteLine(objException);
        }

    }

    /**
     * since the format option with imdisk doesn't seem to work
     * use the fomat X: command via cmd
     * 
     * as I would say in german:
     * "Von hinten durch die Brust ins Auge"
     * **/
    private void formatRAMDisk(){

        string cmdFormatHDD = "format " + MountPoint + "/Q /FS:NTFS";

        SecureString password = new SecureString();
        password.AppendChar('0');
        password.AppendChar('8');
        password.AppendChar('1');
        password.AppendChar('5');

        ProcessStartInfo formatRAMDiskProcess   = new ProcessStartInfo();
        formatRAMDiskProcess.UseShellExecute    = false;
        formatRAMDiskProcess.CreateNoWindow     = true;
        formatRAMDiskProcess.RedirectStandardInput     = true;
        formatRAMDiskProcess.FileName           = "cmd";
        formatRAMDiskProcess.Verb               = "runas";
        formatRAMDiskProcess.UserName           = "Administrator";
        formatRAMDiskProcess.Password           = password;
        formatRAMDiskProcess.Arguments          = "/C " + cmdFormatHDD;
        Process process                         = Process.Start(formatRAMDiskProcess);

        sendCMDInput(process);
    }

    private void sendCMDInput(Process process)
    {
        StreamWriter inputWriter = process.StandardInput;
        inputWriter.WriteLine("J");
        inputWriter.Flush();
        inputWriter.WriteLine("RAMDisk for valueable data");
        inputWriter.Flush();
    }

    public string getMountPoint()
    {
        return MountPoint;
    }
}

Tags:

Windows

Ram