0 out of 3 rated this helpful - Rate this topic

ProcessStartInfo.CreateNoWindow Property

Gets or sets a value indicating whether to start the process in a new window.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

public bool CreateNoWindow { get; set; }
/** @property */
public boolean get_CreateNoWindow ()

/** @property */
public void set_CreateNoWindow (boolean value)

public function get CreateNoWindow () : boolean

public function set CreateNoWindow (value : boolean)

Property Value

true to start the process without creating a new window to contain it; otherwise, false. The default is false.
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
	/// <summary>
	/// Shell for the sample.
	/// </summary>
	class MyProcess
	{
		// These are the Win32 error code for file not found or access denied.
		const int ERROR_FILE_NOT_FOUND =2;
		const int ERROR_ACCESS_DENIED = 5;

		/// <summary>
		/// Prints a file with a .doc extension.
		/// </summary>
		void PrintDoc()
		{
			Process myProcess = new Process();
			
			try
			{
				// Get the path that stores user documents.
				string myDocumentsPath = 
					Environment.GetFolderPath(Environment.SpecialFolder.Personal);

				myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
				myProcess.StartInfo.Verb = "Print";
				myProcess.StartInfo.CreateNoWindow = true;
				myProcess.Start();
			}
			catch (Win32Exception e)
			{
				if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
				{
					Console.WriteLine(e.Message + ". Check the path.");
				} 

				else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
				{
					// Note that if your word processor might generate exceptions
					// such as this, which are handled first.
					Console.WriteLine(e.Message + 
						". You do not have permission to print this file.");
				}
			}
		}


		public static void Main()
		{
			MyProcess myProcess = new MyProcess();
			myProcess.PrintDoc();
		}
	}
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
not quite accurate...

This says "Gets or sets a value indicating whether to start the process in a new window. ", but that's misleading for console apps.  This property translates to a CREATE_NO_WINDOW flag being passed to kernel32!CreateProcess, which means the console app runs without a window at all.

1) To run the child process without any window,

use the CreateNoWindow property and set UseShellExecute.

                ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
                info.CreateNoWindow = true;
                info.UseShellExecute = false;
                Process processChild = Process.Start(info); // no window

 

UseShellExecute=false means that Process.Start basically becomes a direct call to kernel32!CreateProcess. Practically, this means that now you just need to figure out how various ProcessStartInfo properties map to kernel32!CreateProcess flags. Presumably ProcessStartInfo defaults most flags to 0. The default is to share console windows and you need to pass the CREATE_NEW_CONSOLE flag to give the child a new console. 

2) To run the child process in it's own window (a new console window from the parent):
UseShellExecute=true, which is the default value.

                ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
                Process processChild = Process.Start(info); // separate window


3) To run the child process in the parent's console window:

UseShellExecute=false. 

                ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
                info.UseShellExecute = false; // causes consoles to share window
                Process processChild = Process.Start(info);