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?
LVL 2
itniflAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Yes, use a Forms project (Windows Application) without a form.

1. Create a new Windows Application project.

Capture.JPG

2. Remove the auto-generated form.

Capture.JPG

3. Resolve the error in Program.cs.

Form1 no longer exists so we can remove this line.Now you have a blank slate to work with.  From here you can implement your method directly in Program.cs or create a new class; e.g. -
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace EE_Q28740914
{
	static class Program
	{
		static Program()
		{
			try
			{
				Application.ThreadException += OnThreadException;
				AppDomain.CurrentDomain.UnhandledException += OnApplicationUnhandledException;
			}
			catch (Exception ex)
			{
				MessageBox.Show(string.Format("Exception reported in {0} - {1} [{2}]", ex.Source, ex, ex.Message));
			}
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			RunDiskpart(0);
			return;
		}

		private static void RunDiskpart(int diskNumber)
		{
			using (Process p = new Process())
			{
				ProcessStartInfo info = new ProcessStartInfo()
				{
					CreateNoWindow = true,
					FileName = @"C:\Windows\System32\diskpart.exe",
					RedirectStandardInput = true,
					RedirectStandardOutput = true,
					UseShellExecute = true,
					WindowStyle = ProcessWindowStyle.Hidden
				};
				p.StartInfo = info;
				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();
			}
		}

		private static void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
		{
			MessageBox.Show(string.Format("Exception reported in {0} - {1} [{2}]", e.Exception.Source, e.Exception, e.Exception.Message));
		}

		private static void OnApplicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			if (e.IsTerminating)
				MessageBox.Show(e.ExceptionObject.ToString());
		}
	}
}

Open in new window


-saige-
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KraevenCommented:
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
0
itniflAuthor Commented:
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! :)
0
itniflAuthor Commented:
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.
1
KraevenCommented:
LOL ;-)

No problem, I'm just glad you found a solution :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.