process-api-net11
它能做什么
这项技能说明如何使用 .NET 11 中新的 Process API 运行外部程序、捕获其输出并管理其生命周期。适合构建会启动其他程序的 .NET 11 应用的开发者。
适用场景
- ✓当你的 .NET 11 应用需要运行外部程序并捕获其输出时。
- ✓当父应用退出时,子进程应自动关闭的时候。
- ✓当你需要启动分离进程,或控制子进程继承哪些句柄的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet11/skills/process-api-net11/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“在我的 .NET 11 应用中运行一个命令行工具,捕获其文本输出,并确保我的应用退出时它会关闭。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会使用 .NET 11 中新的进程方法来处理等待和输出捕获,并设置你要求的生命周期选项。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Process API Improvements — .NET 11
New APIs added to System.Diagnostics.Process in .NET 11 simplify process management, eliminate boilerplate, and prevent common deadlock patterns when capturing output in .NET 11 or later applications.
When to Use
- Running or orchestrating external processes in a .NET 11 (or later) project.
- Needing to start a process, wait for it to exit, and capture its output/error streams without risking deadlocks (
Process.RunAndCaptureText[Async]). - Wanting to ensure child processes are automatically terminated when the parent process exits (
KillOnParentExit). - Requiring lightweight, low-overhead process creation via
SafeProcessHandleon supported platforms. - Requiring fine-grained control over handle inheritance (
InheritedHandles) or starting detached processes (StartDetached).
When Not to Use
- The project targets .NET 10 or earlier — these APIs are not available before .NET 11.
- The default
Process.Start()is sufficient and does not require output capturing or advanced lifecycle rules.
Target Framework
<TargetFramework>net11.0</TargetFramework>
New APIs
Types
Before using the new convenience methods, note the following types. Use the framework types directly:
| Type | Kind | Read-only properties |
|---|---|---|
ProcessExitStatus |
Sealed class | int ExitCode, bool Canceled, PosixSignal? Signal |
ProcessTextOutput |
Sealed class | ProcessExitStatus ExitStatus, string StandardOutput, string StandardError, int ProcessId |
ProcessOutputLine |
Readonly struct | string Content, bool StandardError |
ProcessExitStatus describes the outcome of a completed process. Interpret ExitCode using the external command's contract; there is no universal Success property. ProcessTextOutput contains the exit status, captured output and error text, and process ID. ProcessOutputLine identifies one output line and whether it came from standard error.
High-Level Convenience APIs
Static Methods
The ProcessStartInfo overloads below require UseShellExecute = false, which is the default for .NET.
Process.Run / Process.RunAsync
Starts a process and waits for it to exit, returning the exit status. Does not capture standard output or error. Passing silent: true discards standard output and error by internally redirecting standard handles to the NUL device. On timeout or cancellation, the process is killed.
public static ProcessExitStatus Run(string fileName, IEnumerable<string>? arguments = null, bool silent = false, TimeSpan? timeout = null)
public static Task<ProcessExitStatus> RunAsync(string fileName, IEnumerable<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default)
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = null)
public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default)
Process.RunAndCaptureText / Process.RunAndCaptureTextAsync
Starts a process, captures both standard output and error, and waits for it to exit. Extremely useful for avoiding deadlocks on stream redirection. On timeout or cancellation, the process is killed.
When using the ProcessStartInfo overloads, the caller is responsible for setting RedirectStandardOutput = true and RedirectStandardError = true on ProcessStartInfo (because BCL APIs cannot modify the input arguments they were given).
public static ProcessTextOutput RunAndCaptureText(string fileName, IEnumerable<string>? arguments = null, TimeSpan? timeout = null)
public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IEnumerable<string>? arguments = null, CancellationToken cancellationToken = default)
public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan? timeout = null)
public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default)
Process.StartAndForget
There is a common misconception that when a process is disposed, it's also being killed. This is not the case, as Process.Dispose only releases the resources associated with the process, but does not kill it.
To make it easier to start a process without the need to worry about disposing it, Process.StartAndForget was introduced. The method starts an executable, retu
常见问题
它适用于 .NET 10 吗?
不适用。这些 API 只存在于 .NET 11 及更高版本中。
默认的 Process.Start 还能用吗?
能。当你不需要捕获输出或新的生命周期规则时,默认方式就很好。
KillOnParentExit 有什么作用?
它会让子进程在父进程结束时自动停止。
详情
作者的原始描述
Provides guidance on the new System.Diagnostics.Process APIs introduced in .NET 11: Process.Run, Process.RunAndCaptureText, Process.StartAndForget, Process.ReadAllText/Bytes/Lines, KillOnParentExit, InheritedHandles, and StartDetached. USE FOR: starting, orchestrating, or capturing output from external processes in applications targeting net11.0 or later. DO NOT USE FOR: applications targeting .NET 10 or earlier, or basic pre-.NET 11 Process.Start usage without new APIs.
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14655
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。