windows 如何以编程方式创建 RAM 磁盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8692521/
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-09 09:04:10  来源:igfitidea点击:

How can I create a RAM disk programmatically?

windowsram

提问by mark

I am not looking for a code that invokes a command line utility, which does the trick. I am actually interested to know the API used to create a RAM disk.

我不是在寻找调用命令行实用程序的代码,它可以解决问题。我实际上很想知道用于创建 RAM 磁盘的 API。

EDIT

编辑

Motivation: I have a third party library which expects a directory name in order to process the files in that directory in a certain way. I have these files zipped in an archive. I wish to extract the archive into a RAM disk and pass the third party the path to the respective directory on that RAM disk. As you can see, memory mapped files are of no use to me.

动机:我有一个第三方库,它需要一个目录名,以便以某种方式处理该目录中的文件。我将这些文件压缩在一个存档中。我希望将存档解压缩到 RAM 磁盘中并将第三方传递到该 RAM 磁盘上相应目录的路径。如您所见,内存映射文件对我没有用

采纳答案by Mike Nakis

There is no API to create a RAM disk per se. You need to write your own device driver which presents a new disk to the system, and make that device driver work with allocated memory.

没有用于创建 RAM 磁盘本身的 API。您需要编写自己的设备驱动程序,为系统提供一个新磁盘,并使该设备驱动程序使用分配的内存工作。

EDIT (after the OP's edit)

编辑(在 OP 编辑​​之后)

What you are trying to do would normally be done using the 'temp' directory, which is supposed to reside on the fastest disk available in the system. So, a system which already has a RAMdisk will probably have its temp environment variable already set to point to a folder on the RAMdisk, and systems without a RAMdisk would simply take the performance penalty, it is their problem.

您尝试执行的操作通常会使用“temp”目录来完成,该目录应该驻留在系统中可用的最快磁盘上。因此,一个已经有 RAMdisk 的系统可能已经将它的临时环境变量设置为指向 RAMdisk 上的文件夹,而没有 RAMdisk 的系统将简单地承担性能损失,这是他们的问题。

Now, if for some reason you cannot rely on the presence of a RAMdisk on a system, AND you absolutely need the fastest performance possible, then I see no option but to install a third-party (perhaps freely downloadable, perhaps even open source) RAM disk, use it temporarily, and then uninstall it. But that's very clunky.

现在,如果由于某种原因您不能依赖系统上的 RAMdisk,并且您绝对需要尽可能快的性能,那么我别无选择,只能安装第三方(也许可以免费下载,甚至可能是开源的) 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 Royi Namir

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

你没有提到语言,所以我的答案是在 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.

内存映射文件包含虚拟内存中文件的内容。文件和内存空间之间的这种映射使应用程序(包括多个进程)能够通过直接读取和写入内存来修改文件。从 .NET Framework 版本 4 开始,您可以使用托管代码访问内存映射文件,其方式与本机 Windows 函数访问内存映射文件的方式相同,如 MSDN 库中的在 Win32 中管理内存映射文件中所述。

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

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

回答by rodix

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

WinFsp允许您使用纯 C++ 或 dotNet C# 以编程方式定义自己的文件系统。示例项目是一个保存在 OS RAM 内存中的文件系统。你也可以看看Dokany

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

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

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

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