gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:10:56 35 4
gpt4 key购买 nike

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

编辑

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

最佳答案

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

关于windows - 如何以编程方式创建 RAM 磁盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8692521/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com