在 C# 中,Process
类是 System.Diagnostics
命名空间下的一个重要类,用于启动、管理和与外部进程进行交互。它提供了丰富的功能,允许你在程序中执行外部应用程序、命令行命令,并监控和控制这些进程的行为。
以下是 Process
类的详细作用及其核心功能:
Process
类的主要作用是启动外部进程。你可以通过它运行任何可执行文件(如 .exe
文件)或命令行命令(如 cmd.exe
)。
using System.Diagnostics;
Process.Start("notepad.exe");
Process.Start("cmd.exe", "/c dir");
Process
类提供了方法来控制进程的启动、停止和等待。
Start()
:启动进程。Kill()
:强制终止进程。WaitForExit()
:等待进程退出。Close()
:释放与进程关联的资源。Process process = Process.Start("notepad.exe");
process.WaitForExit(); // 等待记事本关闭
Console.WriteLine("记事本已关闭");
Process
类允许你重定向进程的标准输入、标准输出和标准错误流,从而可以与进程进行交互。
StandardInput
:向进程发送输入。StandardOutput
:读取进程的输出。StandardError
:读取进程的错误信息。ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c dir";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
Process
类可以获取正在运行的进程的详细信息,包括进程 ID、内存使用情况、启动时间等。
Id
:获取进程的唯一标识符(PID)。ProcessName
:获取进程的名称。StartTime
:获取进程的启动时间。TotalProcessorTime
:获取进程使用的总 CPU 时间。WorkingSet64
:获取进程使用的物理内存量(以字节为单位)。Process currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"进程 ID: {currentProcess.Id}");
Console.WriteLine($"进程名称: {currentProcess.ProcessName}");
Console.WriteLine($"启动时间: {currentProcess.StartTime}");
Console.WriteLine($"内存使用: {currentProcess.WorkingSet64 / 1024} KB");
Process
类提供了静态方法来查找和管理系统中正在运行的进程。
GetProcesses()
:获取所有正在运行的进程。GetProcessById(int processId)
:通过进程 ID 获取特定进程。GetProcessesByName(string processName)
:通过进程名称获取进程列表。Process[] notepadProcesses = Process.GetProcessesByName("notepad");
foreach (Process process in notepadProcesses)
{
Console.WriteLine($"结束进程: {process.ProcessName} (ID: {process.Id})");
process.Kill();
}
通过 ProcessStartInfo
类,你可以配置进程的启动行为,例如:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c echo Hello World";
startInfo.WorkingDirectory = @"C:\";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
Process
类支持事件处理,可以在进程退出或输出数据时触发事件。
Exited
:当进程退出时触发。OutputDataReceived
:当进程输出数据时触发。ErrorDataReceived
:当进程输出错误数据时触发。Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.EnableRaisingEvents = true;
process.Exited += (sender, e) =>
{
Console.WriteLine("进程已退出");
};
process.Start();
在 .NET Core 和 .NET 5+ 中,Process
类支持跨平台操作,可以在 Windows、Linux 和 macOS 上运行。
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "/bin/bash";
startInfo.Arguments = "-c \"ls -l\"";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
Process
类在 C# 中的作用非常广泛,主要包括:
通过 Process
类,你可以将外部应用程序和命令行工具集成到你的 C# 程序中,实现更强大的功能。
本文链接:https://blog.nnwk.net/article/1569
有问题请留言。版权所有,转载请在显眼位置处保留文章出处,并留下原文连接
Leave your question and I'll get back to you as soon as I see it. All rights reserved. Please keep the source and links
友情链接:
子卿全栈
全部评论