Link to home
Start Free TrialLog in
Avatar of itnifl
itniflFlag for Norway

asked on

How do I run diskpart from c# and hide the window?

Here is my code:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("select disk " + diskNumber);
p.StandardInput.WriteLine("clean");
p.StandardInput.WriteLine("active");
p.StandardInput.WriteLine("assign");         
p.StandardInput.WriteLine("exit");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Open in new window


As you see, I have taken a step to make the window hidden. But it still appears.
Any trick to make it actually hide and never show, and still run the code?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Normally if you just add this on top of what you have it should be enough...
p.StartInfo.CreateNoWindow = true;

If this doesn't work I would suggest looking into the Win32API.ShowWindow give it the handle to the window and use the constant Win32API.SW_HIDE
Avatar of itnifl

ASKER

Actually I create the diskpart process via an object, that gets created in the form, like this.

USBMediaCustomizer MediaCustomizer = new USBMediaCustomizer(/*blabla here*/);
MediaCustomizer.PartitionDisks(SelectedUsbDisk.DiskNumber);

Open in new window


I first tested your solution, and it worked. I then tested my old solution with the form, but with this added in my class method USBMediaCustomizer.PartitionDisks(line number two taken from your solution):
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
/* .. and so on */

Open in new window


And that also worked!
Thanks! :)
Avatar of itnifl

ASKER

Kraeven: Sorry, I would have awarded you some points for your answer, but I was writing the explanation you see above while you were writing your answer. So I didn't see your answer before I had already accepted the solution.
LOL ;-)

No problem, I'm just glad you found a solution :-)