Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmable RAM Disk API for .NET?

Tags:

.net

windows

Looking for a RAM disk API (or equivalent set of software to implement) to store file/s temporarily for read/write operations outside the physical hard disk environment.

update Exe files will be written to the RAM disk and executed.

like image 897
John K Avatar asked Mar 08 '10 16:03

John K


3 Answers

A "RAM disk" is an operating system level construct, because it has to implement a file system and device driver to emulate a disk. You can't do this at a library level.

If you want to pursue the OS level, Windows comes with a built-in ramdisk.sys driver.

Otherwise, reading the files into a data structure in your application will have the same performance characteristics as using a RAM disk. Typically, a RAM disk is used when the application is unaware of the fact that it is running off of RAM cached files. If your application is aware of the fact that it wants to do this, just read in the data into your application directly.

like image 191
David Pfeffer Avatar answered Nov 16 '22 02:11

David Pfeffer


ramdisk.sys is a driver that you can get from Microsoft as a driver demo but it apparently isn't guaranteed to work on Windows 7, so good luck with that. You can find other ramdisk implementations, but then you have to install them which means modifying your drive (ie, Step 1: install a sketchy driver).

So this begs the question of what you're trying to do, really. If your goal is to create a sandbox for some executables so that they can't write to the local drive(s), this doesn't sound like the right approach. If the goal is to make a small suite of executables readily available so they run quickly, again, this doesn't seem like the right approach - Windows caches executables ANYWAY, so they second launch is faster. If the goal is to make a small, easy to clean up area for executables to run in, the windows temp directory is supposed to be the place for that. If you're trying to make a system like norton utilities or ghost that give unfettered access to the hard drive without leaving a trace, I question whether or not .NET is the right way to go for that since .NET is going to be hitting your HD hard and heavy anyway. If your goal is to create a virus or a trojan horse that hides its payload in a ram drive, I question your motives.

What are you really trying to do?

Another option instead of a ramdisk is Dokan, which is a user-mode file system driver. Making an in-memory drive from that is straight forward - I did a quick .NET app that made TWAIN scanners appear as drives as a demo, but I found that dokan, at the time I was using it, made my machine fragile: any misstep while I was working with it meant a trip to reboot land. And again, it requires the installation of a driver. Hopefully this has gotten better.

like image 41
plinth Avatar answered Nov 16 '22 01:11

plinth


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;
    }
}
like image 44
Robin Rodricks Avatar answered Nov 16 '22 02:11

Robin Rodricks