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

itnifl
itnifl used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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-

Commented:
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
itniflProgrammer

Author

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! :)
itniflProgrammer

Author

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.

Commented:
LOL ;-)

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial