相关文章推荐
public ref class Process : System::ComponentModel::Component, IDisposable
public ref class Process : IDisposable
public ref class Process : System::ComponentModel::Component
public class Process : System.ComponentModel.Component, IDisposable
public class Process : IDisposable
public class Process : System.ComponentModel.Component
type Process = class
    inherit Component
    interface IDisposable
type Process = class
    interface IDisposable
type Process = class
    inherit Component
Public Class Process
Inherits Component
Implements IDisposable
Public Class Process
Implements IDisposable
Public Class Process
Inherits Component
Object
Process using namespace System; using namespace System::Diagnostics; using namespace System::ComponentModel; int main() Process^ myProcess = gcnew Process; myProcess->StartInfo->UseShellExecute = false; // You can start any process, HelloWorld is a do-nothing example. myProcess->StartInfo->FileName = "C:\\HelloWorld.exe"; myProcess->StartInfo->CreateNoWindow = true; myProcess->Start(); // This code assumes the process you are starting will terminate itself. // Given that it is started without a window so you cannot terminate it // on the desktop, it must terminate itself or you can do it programmatically // from this application using the Kill method. catch ( Exception^ e ) Console::WriteLine( e->Message ); using System; using System.Diagnostics; using System.ComponentModel; namespace MyProcessSample class MyProcess public static void Main() using (Process myProcess = new Process()) myProcess.StartInfo.UseShellExecute = false; // You can start any process, HelloWorld is a do-nothing example. myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"; myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); // This code assumes the process you are starting will terminate itself. // Given that it is started without a window so you cannot terminate it // on the desktop, it must terminate itself or you can do it programmatically // from this application using the Kill method. catch (Exception e) Console.WriteLine(e.Message); Imports System.Diagnostics Imports System.ComponentModel Namespace MyProcessSample Class MyProcess Public Shared Sub Main() Using myProcess As New Process() myProcess.StartInfo.UseShellExecute = False ' You can start any process, HelloWorld is a do-nothing example. myProcess.StartInfo.FileName = "C:\\HelloWorld.exe" myProcess.StartInfo.CreateNoWindow = True myProcess.Start() ' This code assumes the process you are starting will terminate itself. ' Given that it is started without a window so you cannot terminate it ' on the desktop, it must terminate itself or you can do it programmatically ' from this application using the Kill method. End Using Catch e As Exception Console.WriteLine((e.Message)) End Try End Sub End Class End Namespace

以下示例使用 Process 类本身和静态 Start 方法启动进程。

#using <System.dll> using namespace System; using namespace System::Diagnostics; using namespace System::ComponentModel; // Opens the Internet Explorer application. 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); // Opens urls and .html documents using Internet Explorer. void OpenWithArguments() // URLs 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"); // Uses the ProcessStartInfo class to start new processes, // both in a minimized mode. 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(); using System; using System.Diagnostics; using System.ComponentModel; namespace MyProcessSample class MyProcess // Opens the Internet Explorer application. 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); // Opens urls and .html documents using Internet Explorer. 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"); // Uses the ProcessStartInfo class to start new processes, // both in a minimized mode. 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(); Imports System.Diagnostics Imports System.ComponentModel Namespace MyProcessSample Class MyProcess ' Opens the Internet Explorer application. 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 ' Opens URLs and .html documents using Internet Explorer. Sub OpenWithArguments() ' URLs 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 ' Uses the ProcessStartInfo class to start new processes, ' both in a minimized mode. 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 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 End Class End Namespace 'MyProcessSample

下面的 F# 示例定义一个 runProc 函数,该函数启动进程,捕获所有输出和错误信息,并记录进程已运行的毫秒数。 该 runProc 函数有三个参数:要启动的应用程序的名称、要提供给应用程序的参数和起始目录。

open System open System.Diagnostics let runProc filename args startDir : seq<string> * seq<string> = let timer = Stopwatch.StartNew() let procStartInfo = ProcessStartInfo( RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, FileName = filename, Arguments = args match startDir with | Some d -> procStartInfo.WorkingDirectory <- d | _ -> () let outputs = System.Collections.Generic.List<string>() let errors = System.Collections.Generic.List<string>() let outputHandler f (_sender:obj) (args:DataReceivedEventArgs) = f args.Data use p = new Process(StartInfo = procStartInfo) p.OutputDataReceived.AddHandler(DataReceivedEventHandler (outputHandler outputs.Add)) p.ErrorDataReceived.AddHandler(DataReceivedEventHandler (outputHandler errors.Add)) let started = p.Start() with | ex -> ex.Data.Add("filename", filename) reraise() if not started then failwithf "Failed to start process %s" filename printfn "Started %s with pid %i" p.ProcessName p.Id p.BeginOutputReadLine() p.BeginErrorReadLine() p.WaitForExit() timer.Stop() printfn "Finished %s after %A milliseconds" filename timer.ElapsedMilliseconds let cleanOut l = l |> Seq.filter (fun o -> String.IsNullOrEmpty o |> not) cleanOut outputs,cleanOut errors

函数的代码 runProc ImaginaryDevelopment 编写,在 Microsoft 公共许可证 下提供。

Process 组件提供对计算机上正在运行的进程的访问权限。 用最简单的术语说,进程是一个正在运行的应用。 线程是操作系统分配处理器时间的基本单元。 线程可以执行进程代码的任何部分,包括当前由另一个线程执行的部分。

组件 Process 是用于启动、停止、控制和监视应用的有用工具。 可以使用 Process 组件获取正在运行的进程的列表,也可以启动新进程。 组件 Process 用于访问系统进程。 Process 组件初始化后,它可用于获取有关正在运行的进程的信息。 此类信息包括线程集、加载的模块 (.dll 和.exe文件) ,以及进程正在使用的内存量等性能信息。

此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try / finally 块中调用其 Dispose 方法。 若要间接释放类型,请使用 using (在 C# 中)或 Using (在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。

使用不受信任的数据调用此类中的方法存在安全风险。 仅使用受信任的数据调用此类中的方法。 有关详细信息,请参阅 验证所有输入

32 位进程无法访问 64 位进程的模块。 如果尝试从 32 位进程获取有关 64 位进程的信息,则会出现异常 Win32Exception 。 另一方面,64 位进程可以访问 32 位进程的模块。

进程组件一次获取有关一组属性的信息。 在 Process 组件获取有关任何组的一个成员的信息后,它将缓存该组中其他属性的值,并且不会获取有关组其他成员的新信息,直到调用 Refresh 方法。 因此,不保证属性值比上次调用 Refresh 方法更新。 组细分取决于操作系统。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 路径中没有 ,并且使用引号添加它: path = %path%;"c:\mypath" ,则必须在启动时完全限定中的任何 c:\mypath 进程。

系统进程通过其进程标识符在系统上唯一标识。 与许多 Windows 资源一样,进程也由其句柄标识,该句柄在计算机上可能不是唯一的。 句柄是资源标识符的泛型术语。 操作系统保留进程句柄,即使进程已退出,也可以通过 Handle 组件的 属性 Process 访问该句柄。 因此,可以获取进程的管理信息,例如 ExitCode (通常为零表示成功,或者) 和 ExitTime 的非零错误代码。 句柄是一种极其有价值的资源,因此泄漏句柄比泄漏内存更严重。

此类包含应用于所有成员的类级别的链接需求和继承需求。 SecurityException 当直接调用方或派生类没有完全信任权限时,将引发 。 有关安全要求的详细信息,请参阅 链接需求

.NET Core 说明

在.NET Framework中 Process , 类默认使用 Console 输入、输出和错误流的编码,通常为代码页编码。 例如,在区域性为英语 (美国) 的系统上,代码页 437 是 类的默认编码 Console 。 但是,.NET Core 可能只提供这些编码的有限子集。 如果是这种情况,则使用 Encoding.UTF8 作为默认编码。

Process 如果 对象依赖于特定的代码页编码,则仍然可以在调用任何 Process 方法 之前 执行以下操作来使其可用:

  • 向项目添加对System.Text.Encoding.CodePages.dll程序集的引用。

  • EncodingProvider 从 属性中 CodePagesEncodingProvider.Instance 检索 对象。

  • EncodingProvider 对象传递给 Encoding.RegisterProvider 方法,使编码提供程序支持的其他编码可用。

    然后, Process 类将自动使用默认系统编码而不是 UTF8,前提是在调用任何 Process 方法之前已注册编码提供程序。

  •  
    推荐文章