2

I'm trying to create a RAM Directory via imdisk in C#. Since the cmd command is something like:
imdisk -a -s 512M -m X: -p "/fs:ntfs /q /y"
I looked up how to process cmd commands with C# and found several hints regarding ProcessStartInfo(). This class works almost the way I intend it to, but since imdisk needs administrator priviliges I'm kinda stuck. Even though the code block is executed without exceptions, I don't see any new devices within the Windows Explorer.

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

I hope someone can give me a little hint, right now I'm out of ideas.

Robert Schröder
  • 169
  • 2
  • 11
  • Well since c# should start cmd first and start imdisk via console command, it seems to be the right approach. But even if I try your suggestion, nothing changes. – Robert Schröder Dec 06 '12 at 09:21
  • @RobertSchröder pretty rude not to even vote up my answer when it was at least partially correct. I won't be answering any of your questions again. – Jack Hughes Dec 06 '12 at 12:00
  • @JackHughes: My sincerest apologies, I hope I didn't upset you that much. Since I'm pretty new to StackOverflow (I joined yesterday) I don't know all the functions or customs. Hopefully you can forgive me my lack of politeness. PS.: I just tried to give you some Internetpoints, but it seems that I need 15 points myself to vote. – Robert Schröder Dec 06 '12 at 14:56
  • just up voted you with internetpoints, now you've got 6 :) Just 9 to go :) – Jack Hughes Dec 06 '12 at 15:56
  • Thanks, as soon as I'm able to vote I will give you the deserved credit :) – Robert Schröder Dec 06 '12 at 16:20
  • Up voted your other answer... you've now got a whole 16 internetpoints ;) – Jack Hughes Dec 06 '12 at 16:26
  • This was one of the most useful things I have come across in a while, Another upvote from me. Kudos. – The Lazy Coder Apr 22 '21 at 07:47

2 Answers2

3

Well I solved my problem in a different way. Somehow it seems that imdisk didn't format the new RamDisk the way it should and therefor no disk were created. As soon as I deleted the formatting option the disk is created and needs to be formatted. Therefore I started another process and used the cmd command "format Drive:"

For anyone who is interested, my solution is as follows:

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;
    }
}
Robert Schröder
  • 169
  • 2
  • 11
  • Nice piece of code Thanks. BTW the format option is working when using: imdisk -a -s 512M -m X: -p "/fs:ntfs /q /y" – Martin Sep 13 '13 at 10:31
  • `-p "..."` only works if the calling executable is running with `Administrator` privileges – Loathing Mar 15 '16 at 05:31
1

Doesn't cmd.exe need to have the /C command line option passed through to run a command passed through as an argument? May well be that cmd.exe is just ignoring what you're passing through in procStartInfo.Arguments because you haven't prepended "/C " onto the front of the Arguments.

Jack Hughes
  • 5,514
  • 4
  • 27
  • 32
  • Thanks, that was at least one error in my code. Now I can see the correct string within the cmd window, but the device is still not created. – Robert Schröder Dec 06 '12 at 09:16