How to create simple volume from unallocated partition

Refresh

1 weeks ago

Views

15 time

0

I'm trying to open a place on my harddisk to store some licensing files.

So far I have tried diskpart. It looks easy to use but I could not format the unallocated partition with diskpart. I have found a way to create the unallocated space but I have to format it to use(correct me if I am wrong here. I'm really new on disk partition stuff)

This is my method to select the right volume. I've take it from here and it's working good. Link : C# and diskpart: how to select by disk label and not by a number? and the code I am using is this :

public int GetIndexOfDrive(string drive)
{
    drive = drive.Replace(":", "").Replace(@"\", "");

    // execute DiskPart programatically
    Process process = new Process();
    process.StartInfo.FileName = "diskpart.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.StandardInput.WriteLine("list volume");
    process.StandardInput.WriteLine("exit");
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // extract information from output
    string table = output.Split(new string[] { "DISKPART>" },         StringSplitOptions.None)[1];
    var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
    for (int i = 3; i < rows.Length; i++)
    {
        if (rows[i].Contains("Volume"))
        {
            int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
            string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];

            if (label.Equals(drive))
            {
                return index;
            }
        }
    }

    return -1;
}

once I get the index I run my own code to shrink that selected volume with this code :

Process DiskPartProc = new Process();                                  
DiskPartProc.StartInfo.CreateNoWindow = true;
DiskPartProc.StartInfo.UseShellExecute = false;                        
DiskPartProc.StartInfo.RedirectStandardOutput = true;                  
DiskPartProc.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe"; 
DiskPartProc.StartInfo.RedirectStandardInput = true;                   
DiskPartProc.Start();
DiskPartProc.StandardInput.WriteLine("select volume "+index);
DiskPartProc.StandardInput.WriteLine("shrink desired=16 minimum=16");
DiskPartProc.StandardInput.WriteLine("exit");                          
string output = DiskPartProc.StandardOutput.ReadToEnd();               
DiskPartProc.WaitForExit();

Once I do this the result is like this :

http://prntscr.com/mjwg0t (Picture of an unallocated partition only)

I can right click on it and create new simple volume from that unallocated partition but I have to do this with diskpart commands. Can someone tell me which diskpart commands do I have to use to achieve this? And how can I get detailed information about this volume?

0 answers