Avatar of itnifl
itnifl
Flag 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?
C#.NET ProgrammingProgramming

Avatar of undefined
Last Comment
Kraeven

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kraeven

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
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! :)
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Kraeven

LOL ;-)

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