在本文中,我们展示了如何使用C#语言处理进程。
Process
提供对本地和远程进程的访问,并使开发人员能够启动和停止本地系统进程。ProcessStartInfo
指定了一组值,当我们开始一个过程。
Process
类是System.Diagnostics
命名空间的一部分。
C#进程简单例子
在第一个示例中,我们启动一个显示文件内容的控制台命令。
using System.Diagnostics; Process.Start("cat", @"C:\Users\Jano\Documents\words.txt");
该示例使用cat
命令输出words.txt
文件的内容。默认情况下,该命令不是Windows操作系统的一部分;它与git工具一起安装(请参阅gitforwindows.org)。
Process.Start("cat", @"C:\Users\Jano\Documents\words.txt");
该进程使用Start
方法启动。
$ dotnet run sky cloud falcon owl crane
C#进程运行程序
在下面的示例中,我们运行一个GUI程序。
using System.Diagnostics; using var process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.Start();
在示例中,我们运行记事本程序。它是一个标准的小型文本编辑器。
process.StartInfo.FileName = "notepad.exe";
StartInfo.FileName
属性下注或设置要启动的应用程序或文档。
using System.Diagnostics; using var process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.StartInfo.Arguments = @"C:\Users\Jano\Documents\words.txt"; process.Start();
我们使用StartInfo.Arguments
来传递要打开的文件名。
C#启动和终止程序
下一个示例启动一个程序并在几秒钟后终止它。
using System.Diagnostics; using var process = Process.Start("notepad.exe"); Thread.Sleep(3000); process.Kill();
该示例启动记事本,休眠三秒钟,然后使用Kill
方法终止进程。
C#Process.GetProcessesByName
Process.GetProcessesByName
创建一组新的Process
组件,并将它们与共享指定进程名称的现有进程资源相关联。
using System.Diagnostics; Process[] processes = Process.GetProcessesByName("Firefox"); Console.WriteLine("{0} Firefox processes", processes.Length); Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
在示例中,我们找到属于Firefox的所有进程。我们列出了它们的ID和进程名称。
Process[] processes = Process.GetProcessesByName("Firefox");
我们得到名为“Firefox”的进程数组。
Console.WriteLine("{0} Firefox processes", processes.Length);
我们打印找到的进程数。
Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
我们使用ForEach
方法列出进程。
$ dotnet run 12 Firefox processes Process: firefox Id: 10056 Process: firefox Id: 13016 Process: firefox Id: 12944 Process: firefox Id: 10124 Process: firefox Id: 15556 ...
C#Process.GetProcesses
Process.GetProcesses
创建一组新的Process
组件并将它们与现有的进程资源相关联。
using System.Diagnostics; Process[] processes = Process.GetProcesses(); Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
示例列出所有进程。
Process[] processes = Process.GetProcesses();
我们得到进程数组。
Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
我们遍历数组并打印进程名称和ID。
C#处理重定向输出
StandardOutput
属性获取用于读取应用程序文本输出的流。
using System.Diagnostics; var psi = new ProcessStartInfo(); psi.FileName = "ls"; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; using var process = Process.Start(psi); using StreamReader reader = process.StandardOutput; string data = reader.ReadToEnd(); File.WriteAllText("output.txt", data);
在示例中,我们将ls
命令的输出重定向到output.txt
文件。
psi.UseShellExecute = false; psi.RedirectStandardOutput = true;
将UseShellExecute
设置为false
使我们能够重定向输入、输出和错误流。(在此上下文中,shell指的是图形shell而不是命令shell,例如bash或sh。)
using var process = Process.Start(psi);
我们使用提供的信息启动流程。
using StreamReader reader = process.StandardOutput;
我们得到标准输出的StreamReader
。
string data = reader.ReadToEnd();
我们使用ReadToEnd
方法读取所有数据。
File.WriteAllText("output.txt", data);
最后,我们将数据写入文件。
在本文中,我们使用了C#中的进程。
列出所有C#教程。