Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Process Class

Provides access to local and remote processes and enables you to start and stop local system processes.

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

public class Process : Component
public class Process extends Component
public class Process extends Component
Not applicable.

NoteNote:

Starting with the .NET Framework version 2.0, the ability to reference performance counter data on other computers has been eliminated for many of the .NET Framework methods and properties. This change was made to improve performance and to enable non-administrators to use the Process class. As a result, some applications that did not get exceptions in earlier versions of the .NET Framework may now get a NotSupportedException. The methods and properties affected are too numerous to list here, but the exception information has been added to the affected member topics.

A Process component provides access to a process that is running on a computer. A process, in the simplest terms, is a running application. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the code of the process, including parts currently being executed by another thread.

The Process component is a useful tool for starting, stopping, controlling, and monitoring applications. Using the Process component, you can obtain a list of the processes that are running, or you can start a new process. A Process component is used to access system processes. After a Process component has been initialized, it can be used to obtain information about the running process. Such information includes the set of threads, the loaded modules (.dll and .exe files), and performance information such as the amount of memory the process is using.

If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. Otherwise, the system will not find the path. For example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

The process component obtains information about a group of properties all at once. After the Process component has obtained information about one member of any group, it will cache the values for the other properties in that group and not obtain new information about the other members of the group until you call the Refresh method. Therefore, a property value is not guaranteed to be any newer than the last call to the Refresh method. The group breakdowns are operating-system dependent.

A system process is uniquely identified on the system by its process identifier. Like many Windows resources, a process is also identified by its handle, which might not be unique on the computer. A handle is the generic term for an identifier of a resource. The operating system persists the process handle, which is accessed through the Handle property of the Process component, even when the process has exited. Thus, you can get the process's administrative information, such as the ExitCode (usually either zero for success or a nonzero error code) and the ExitTime. Handles are an extremely valuable resource, so leaking handles is more virulent than leaking memory.

NoteNote:

This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands.

The following example uses an instance of the Process class to start a process.

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();
		}
	}
}

The following example uses the Process class itself and a static Start method to start a process.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
	/// <summary>
	/// Shell for the sample.
	/// </summary>
	class MyProcess
	{
	   
		/// <summary>
		/// Opens the Internet Explorer application.
		/// </summary>
		void OpenApplication(string myFavoritesPath)
		{
			// Start Internet Explorer. Defaults to the home page.
			Process.Start("IExplore.exe");
				    
		    // Display the contents of the favorites folder in the browser.
		    Process.Start(myFavoritesPath);
 
		}
		
		/// <summary>
		/// Opens urls and .html documents using Internet Explorer.
		/// </summary>
		void OpenWithArguments()
		{
			// url's are not considered documents. They can only be opened
			// by passing them as arguments.
			Process.Start("IExplore.exe", "www.northwindtraders.com");
			
			// Start a Web page using a browser associated with .html and .asp files.
			Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
			Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
		}
		
		/// <summary>
		/// Uses the ProcessStartInfo class to start new processes, both in a minimized 
		/// mode.
		/// </summary>
		void OpenWithStartInfo()
		{
			
			ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
			startInfo.WindowStyle = ProcessWindowStyle.Minimized;
			
			Process.Start(startInfo);
			
			startInfo.Arguments = "www.northwindtraders.com";
			
			Process.Start(startInfo);
			
		}

		static void Main()
		{
            		// Get the path that stores favorite links.
            		string myFavoritesPath = 
                	Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
                
            		MyProcess myProcess = new MyProcess();
         
			myProcess.OpenApplication(myFavoritesPath);
			myProcess.OpenWithArguments();
			myProcess.OpenWithStartInfo();

       		}	
	}
}

  • LinkDemand  for full trust for the immediate caller. This class cannot be used by partially trusted code.
  • InheritanceDemand  for full trust for inheritors. This class cannot be inherited by partially trusted code.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft