Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Ermöglicht den Zugriff auf lokale Prozesse und Remoteprozesse und das Starten und Anhalten lokaler Systemprozesse.
Namespace: System.Diagnostics
Assembly: System (in system.dll)
Syntax
'Declaration
Public Class Process
Inherits Component
'Usage
Dim instance As Process
public class Process : Component
public ref class Process : public Component
public class Process extends Component
public class Process extends Component
Hinweise
Hinweis
Das auf diese Klasse angewendete HostProtectionAttribute-Attribut besitzt den Resources-Eigenschaftenwert Synchronization | SharedState | ExternalProcessMgmt | SelfAffectingProcessMgmt. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen (die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder eines URL in einem Browser gestartet werden). Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute.
Eine Process-Komponente gewährt den Zugriff auf einen Prozess, der auf einem Computer ausgeführt wird. Ein Prozess ist, vereinfacht ausgedrückt, eine Anwendung während der Ausführung. Ein Thread ist die Basiseinheit, für die das Betriebssystem Prozessorzeit reserviert. Ein Thread kann beliebige Teile des Prozesscodes ausführen. Dies umfasst auch Teile, die derzeit von einem anderen Thread ausgeführt werden.
Die Process-Komponente ist ein nützliches Tool für das Starten, Anhalten, Steuern und Überwachen von Anwendungen. Mithilfe der Process-Komponente können Sie eine Liste der Prozesse abrufen, die ausgeführt werden, oder einen neuen Prozess starten. Mit einer Process-Komponente können Sie auf Systemprozesse zugreifen. Nachdem eine Process-Komponente initialisiert wurde, kann sie zum Abrufen von Informationen über den laufenden Prozess verwendet werden. Diese Informationen beinhalten die Gruppe von Threads, die geladenen Module (DLL- und EXE-Dateien) sowie Leistungsinformationen wie die Größe des vom Prozess beanspruchten Speicherplatzes.
Wenn Sie im System eine Pfadvariable mit Anführungszeichen deklariert haben, müssen Sie diesen Pfad voll qualifizieren, um einen an dieser Position gefundenen Prozess zu starten. Andernfalls findet das System den Pfad nicht. Wenn z. B. c:\mypath nicht im Pfad enthalten ist und von Ihnen unter Verwendung von Anführungszeichen hinzugefügt wird, wie in path = %path%;"c:\mypath", müssen Sie jeden Prozess in c:\mypath beim Starten voll qualifizieren.
Die Prozesskomponente ruft Informationen über eine Gruppe von Eigenschaften gleichzeitig ab. Wenn die Process-Komponente Informationen zu einem Member einer beliebigen Gruppe abgerufen hat, werden die Werte der anderen Eigenschaften in dieser Gruppe zwischengespeichert und keine neuen Informationen zu anderen Membern der Gruppe abgerufen, bis Sie die Refresh-Methode aufrufen. Daher ist ein Eigenschaftenwert nicht unbedingt aktueller als der letzte Aufruf der Refresh-Methode. Die Einteilung der Gruppen ist vom Betriebssystem abhängig.
Systemprozesse werden im System durch die Prozess-ID eindeutig bezeichnet. Wie viele Windows-Ressourcen kann ein Prozess auch über sein Handle bezeichnet werden, das auf dem Computer nicht unbedingt eindeutig sein muss. Handle ist der Oberbegriff für den Bezeichner einer Ressource. Das Betriebssystem behält das Prozesshandle bei, auf das selbst bei beendetem Prozess über die Handle-Eigenschaft der Process-Komponente zugegriffen werden kann. Auf diese Weise können Sie die administrativen Informationen des Prozesses abrufen, z. B. ExitCode (meist 0 bei Erfolg oder ein Fehlercode ungleich 0) und ExitTime. Handles sind eine besonders nützliche Ressource. Daher sind Handleverluste gravierender als Speicherverluste.
Hinweis
Diese Klasse enthält auf Klassenebene einen Verknüpfungsaufruf und eine Vererbungsforderung, die für alle Member gelten. Eine SecurityException wird ausgelöst, wenn der direkte Aufrufer oder die abgeleitete Klasse nicht über vollständige vertrauenswürdige Berechtigungen verfügen. Ausführliche Informationen über Sicherheitsforderungen finden Sie unter Verknüpfungsaufrufe und Vererbungsforderungen.
Beispiel
Im folgenden Beispiel wird eine Instanz der Process-Klasse zum Starten eines Prozesses verwendet.
Imports System
Imports System.Diagnostics
Imports 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.
Private ERROR_FILE_NOT_FOUND As Integer = 2
Private ERROR_ACCESS_DENIED As Integer = 5
'/ <summary>
'/ Prints a file with a .doc extension.
'/ </summary>
Sub PrintDoc()
Dim myProcess As New Process()
Try
' Get the path that stores user documents.
Dim myDocumentsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
myProcess.StartInfo.FileName = myDocumentsPath + "\MyFile.doc"
myProcess.StartInfo.Verb = "Print"
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
Catch e As Win32Exception
If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
Console.WriteLine((e.Message + ". Check the path."))
Else
If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
' 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."))
End If
End If
End Try
End Sub 'PrintDoc
Public Shared Sub Main()
Dim myProcess As New MyProcess()
myProcess.PrintDoc()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
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();
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
// These are the Win32 error code for file not found or access denied.
#define ERROR_FILE_NOT_FOUND 2
#define ERROR_ACCESS_DENIED 5
int main()
{
Process^ myProcess = gcnew Process;
try
{
// Get the path that stores user documents.
String^ myDocumentsPath = Environment::GetFolderPath( Environment::SpecialFolder::Personal );
myProcess->StartInfo->FileName = String::Concat( 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( "{0}. Check the path.", e->Message );
}
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( "{0}. You do not have permission to print this file.", e->Message );
}
}
}
Im folgenden Beispiel wird die Process-Klasse selbst und eine statische Start-Methode zum Starten eines Prozesses verwendet.
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
_
'/ <summary>
'/ Shell for the sample.
'/ </summary>
Class MyProcess
'/ <summary>
'/ Opens the Internet Explorer application.
'/ </summary>
Public Sub OpenApplication(myFavoritesPath As String)
' 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)
End Sub 'OpenApplication
'/ <summary>
'/ Opens urls and .html documents using Internet Explorer.
'/ </summary>
Sub 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")
End Sub 'OpenWithArguments
'/ <summary>
'/ Uses the ProcessStartInfo class to start new processes, both in a minimized
'/ mode.
'/ </summary>
Sub OpenWithStartInfo()
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startInfo)
startInfo.Arguments = "www.northwindtraders.com"
Process.Start(startInfo)
End Sub 'OpenWithStartInfo
Shared Sub Main()
' Get the path that stores favorite links.
Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
Dim myProcess As New MyProcess()
myProcess.OpenApplication(myFavoritesPath)
myProcess.OpenWithArguments()
myProcess.OpenWithStartInfo()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
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();
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
/// <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 = gcnew ProcessStartInfo( "IExplore.exe" );
startInfo->WindowStyle = ProcessWindowStyle::Minimized;
Process::Start( startInfo );
startInfo->Arguments = "www.northwindtraders.com";
Process::Start( startInfo );
}
int main()
{
// Get the path that stores favorite links.
String^ myFavoritesPath = Environment::GetFolderPath( Environment::SpecialFolder::Favorites );
OpenApplication( myFavoritesPath );
OpenWithArguments();
OpenWithStartInfo();
}
.NET Framework-Sicherheit
- SecurityPermission zum Aufrufen von Process-Membern. Anforderungswert: LinkDemand, Benannte Berechtigungssätze: FullTrust.
- SecurityPermission zum Ableiten von der Process-Klasse. Anforderungswert: InheritanceDemand, Benannte Berechtigungssätze: FullTrust.
Vererbungshierarchie
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Diagnostics.Process
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0
Siehe auch
Referenz
Process-Member
System.Diagnostics-Namespace
Start
ProcessStartInfo
CloseMainWindow
Kill
ProcessThread