如何使用imdisk和C#创建RAM磁盘?

时间:2012-12-05 16:23:17

标签: c# processstartinfo

我正在尝试通过C#中的imdisk创建一个RAM目录。由于cmd命令类似于:
imdisk -a -s 512M -m X: -p "/fs:ntfs /q /y"
我查找了如何使用C#处理cmd命令,并找到了一些关于ProcessStartInfo()的提示。这个类几乎按照我的意图工作,但由于imdisk需要管理员权限,我有点卡住了。即使代码块没有例外地执行,我也没有在Windows资源管理器中看到任何新设备。

        try
        {
            string initializeDisk   = "imdisk -a ";
            string imdiskSize       = "-s 1024M ";
            string mountPoint       = "-m "+ MountPoint + " ";
            string formatHdd        = "-p '/fs:ntfs /q /y' ";

            SecureString password = new SecureString();
            password.AppendChar('0');
            password.AppendChar('8');
            password.AppendChar('1');
            password.AppendChar('5');
            ProcessStartInfo procStartInfo = new ProcessStartInfo();
            procStartInfo.UseShellExecute   = false;
            procStartInfo.CreateNoWindow    = true;
            procStartInfo.RedirectStandardError = true;
            procStartInfo.FileName          = "cmd";
            procStartInfo.Verb              = "runas";
            procStartInfo.UserName          = "Admin";
            procStartInfo.Password          = password;
            procStartInfo.Arguments         = initializeDisk + imdiskSize + mountPoint + formatHdd;
            Process.Start(procStartInfo);

        catch (Exception objException)
        {
            Console.WriteLine(objException);
        }

我希望有人可以给我一点提示,现在我已经没有想法了。

2 个答案:

答案 0 :(得分:2)

我以不同的方式解决了我的问题。不知何故,似乎imdisk没有按照它应该的方式格式化新的RamDisk,因此没有创建磁盘。一旦删除格式化选项,就会创建磁盘并需要格式化。因此,我启动了另一个进程并使用了cmd命令“format Drive:”

对于任何有兴趣的人,我的解决方案如下:

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

答案 1 :(得分:1)

不是cmd.exe需要通过/ C命令行选项来运行作为参数传递的命令吗?很可能cmd.exe只是忽略了你在procStartInfo.Arguments中传递的内容,因为你还没有在" / C"在论据的前面。