Share via


Run Diskpart from within c#

Question

Thursday, March 5, 2015 10:17 PM

How can I run Diskpart from within a from application as follows

diskpart

list disk

select disk

...

All replies (4)

Friday, March 6, 2015 10:56 AM âś…Answered

Please have another look at the code in the blog post it is running DiskPart.exe not cmd.exe...

Process p = new Process();                                    // new instance of Process class
p.StartInfo.UseShellExecute = false;                          // do not start a new shell
p.StartInfo.RedirectStandardOutput = true;                    // Redirects the on screen results
p.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";   // executable to run
p.StartInfo.RedirectStandardInput = true;                     // Redirects the input commands
p.Start();                    

Friday, March 6, 2015 12:08 AM

You can use Process to start and control external processes. There is a blog post here on doing so with DiskPart. Hope it helps.


Friday, March 6, 2015 9:38 AM

I had already used what you suggested but it doesnot work.

I already tried to run

Process.Start("cmd.exe", "/k diskpart /s myscript.txt>> output.log");      

with my script as

   diskpart
   list disk
   select disk 1
   clean
   create partition primary
   select partition 1
   active
   format fs=ntfs quick
   assign
   exit

but this doesnt work too    


Friday, March 6, 2015 11:19 AM

thanks alot

the reason that it doesnot work is that

when i use diskpart.exe in another directory even if it is marked as run as administrator

            p.StartInfo.FileName = @"C:\command1\command1\bin\Debug\diskpart.exe";   // executable to run

it doesn't work

but the following works fine

            p.StartInfo.FileName = @"C:\windows\system32\diskpart.exe";   // executable to run