windows .NET 的可编程 RAM 磁盘 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:05:36  来源:igfitidea点击:

Programmable RAM Disk API for .NET?

.netwindows

提问by John K

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.

寻找 RAM 磁盘 API(或要实现的等效软件集)来临时存储文件,以便在物理硬盘环境之外进行读/写操作。

updateExe files will be written to the RAM disk and executed.

updateexe 文件将写入 RAM 磁盘并执行。

采纳答案by David Pfeffer

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.

“RAM 磁盘”是操作系统级别的构造,因为它必须实现文件系统和设备驱动程序来模拟磁盘。您不能在库级别执行此操作。

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

如果你想追求操作系统级别,Windows 自带了一个内置的 ramdisk.sys 驱动程序。

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.

否则,将文件读入应用程序中的数据结构将具有与使用 RAM 磁盘相同的性能特征。通常,当应用程序不知道它正在运行 RAM 缓存文件这一事实时,使用 RAM 磁盘。如果您的应用程序知道它想要这样做,只需将数据直接读入您的应用程序即可。

回答by TheFlash

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

ImDisk是一个 RAM 磁盘应用程序,可从内存扇区创建虚拟驱动器,并具有可从 .NET 调用的 API。

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;
    }
}

回答by plinth

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).

ramdisk.sys 是一个驱动程序,您可以从 Microsoft 获得作为驱动程序演示,但它显然不能保证在 Windows 7 上工作,所以祝你好运。您可以找到其他 ramdisk 实现,但是您必须安装它们,这意味着修改您的驱动器(即,步骤 1:安装一个粗略的驱动程序)。

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.

所以这引出了你想要做什么的问题,真的。如果您的目标是为某些可执行文件创建一个沙箱,以便它们无法写入本地驱动器,那么这听起来不是正确的方法。如果目标是使一小组可执行文件随时可用,以便它们快速运行,那么这似乎不是正确的方法 - Windows 无论如何都会缓存可执行文件,因此它们的第二次启动速度更快。如果目标是为运行可执行文件创建一个小而易于清理的区域,则 Windows 临时目录应该是该位置。如果您正在尝试制作一个像诺顿实用程序或 ghost 这样的系统,可以不受限制地访问硬盘而不留下任何痕迹,我怀疑 .NET 是否是正确的方法,因为 .NET 将会受到冲击反正你的高清又硬又重。

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.

另一个替代 ramdisk 的选项是Dokan,它是一个用户模式文件系统驱动程序。用它制作内存驱动器很简单 - 我做了一个快速的 .NET 应用程序,它使 TWAIN 扫描仪显示为驱动器作为演示,但我发现当我使用它时,dokan 使我的机器变得脆弱:任何我在使用它时的失误意味着重新启动土地的旅行。同样,它需要安装驱动程序。希望这已经变得更好。