AsyncProcess.Sharp 1.0.1905181338
dotnet add package AsyncProcess.Sharp --version 1.0.1905181338
NuGet\Install-Package AsyncProcess.Sharp -Version 1.0.1905181338
该命令应在 Visual Studio 的包管理器控制台中使用,因为它使用 NuGet 模块的 Install-Package 版本。
<PackageReference Include="AsyncProcess.Sharp" Version="1.0.1905181338" />
对于支持 包引用 的项目,将此 XML 节复制到项目文件中以引用此代码包。
paket add AsyncProcess.Sharp --version 1.0.1905181338
NuGet 团队不提供对此客户端的支持。请联系其 维护者 以获取支持。
#r "nuget: AsyncProcess.Sharp, 1.0.1905181338"
#r 指令可用于 F# Interactive 和 Polyglot Notebooks。将此内容复制到交互式工具或脚本的源代码中以便引用代码包。
// Install AsyncProcess.Sharp as a Cake Addin #addin nuget:?package=AsyncProcess.Sharp&version=1.0.1905181338 // Install AsyncProcess.Sharp as a Cake Tool #tool nuget:?package=AsyncProcess.Sharp&version=1.0.1905181338
NuGet 团队不提供对此客户端的支持。请联系其 维护者 以获取支持。
AsyncProcess
关于
此库是 System.Diagnostics.Process
的一个非常简单的包装器,允许进行异步/等待操作。您不是创建并启动一个 Process
,而是创建一个 AsyncProcess
并等待该进程的运行。
目前库不支持所有与 System.Diagnostics.Process
有关的操作,但允许以下操作:
- 运行一个进程并异步等待进程结束。
- 捕获标准输出流。
- 捕获标准错误流。
- 设置
CreateNoWindow
属性。 - 设置启动参数
- 设置当前工作目录。
- 取消异步进程。
AsyncProcess
的所有运行方法都返回一个 AsyncProcessResult
。此结果包含结果的状 态(即进程是否完成或是否发生错误)。如果进程成功完成,则结果将包含进程的退出代码。如果进程失败,则 Exception
属性将设置为实现进程时发生的异常。
在构造AsyncProcess
对象时,您可以传递一个可选 cancellation token,异步过程将尊重该 token。但是,这个过程可能不会立即退出。当设置 cancellation token 时,将会对内部 System.Diagnostics.Process
执行 Kill
方法,我们将等待直到过程 HasExited
属性返回 true。
示例
运行过程并使用过程的退出代码
using (var process = new AsyncProcess(new AsyncProcessStartInfo("ping.exe", "www.github.com")))
{
var result = await process.Run();
Console.WriteLine($"The pinging of github resulted in the exit code: {result.ExitCode}");
}
运行过程并处理错误
using (var process = new AsyncProcess(new AsyncProcessStartInfo("missing-file.exe")))
{
var result = await process.Run();
if (result.Exception != null)
{
throw new Exception($"Failed to run 'missing-file.exe', result: ${result.CompletionState}",
result.Exception);
}
}
使用回调捕获过程的输出
var startInfo = new AsyncProcessStartInfo("ping.exe", "www.github.com")
{
OnStandardOutputReceived = message =>
{
Console.WriteLine($"Info: {message}");
},
OnStandardErrorReceived = message =>
{
Console.WriteLine($"Error: {message}");
}
};
using (var process = new AsyncProcess(startInfo))
{
var result = await process.Run();
Console.WriteLine($"The pinging of github resulted in the exit code: {result.ExitCode}");
}
将过程的输出捕获并存储在结果中
var startInfo = new AsyncProcessStartInfo("ping.exe", "www.github.com")
{
CaptureOutputToProcessResult = ProcessOutputCaptureMode.Both
};
using (var process = new AsyncProcess(startInfo))
{
var result = await process.Run();
Console.WriteLine($"The pinging of github resulted in the exit code: {result.ExitCode}");
Console.WriteLine($"Standard Output: {result.StandardOutput}");
Console.WriteLine($"Standard Error: {result.StandardError}");
}
辅助方法
有一些辅助方法,允许单行使用 AsyncProcess 类
await AsyncProcess.Run("notepad.exe")
/// <summary>
/// Create a new process, executing the specified file.
/// </summary>
/// <param name="fileName">The file to use when launching the process.</param>
/// <returns>An awaitable task containing the result of the process run.</returns>
public static async Task<AsyncProcessResult> Run(string fileName)
await AsyncProcess.Run("notepad.exe", @"C:\Windows\System32\drivers\etc\hosts")
/// <summary>
/// Create a new process, executing the specified file with the provided launch arguments.
/// </summary>
/// <param name="fileName">The file to use when launching the process.</param>
/// <param name="arguments">The arguments to use when launching the process.</param>
/// <returns>An awaitable task containing the result of the process run.</returns>
public static async Task<AsyncProcessResult> Run(string fileName, string arguments)
await AsyncProcess.Run(new AsyncProcessStartInfo("notepad.exe"))
/// <summary>
/// Create a new process, executing using the provided start info.
/// </summary>
/// <param name="startInfo">The information used when starting the new process.</param>
/// <returns>An awaitable task containing the result of the process run.</returns>
public static async Task<AsyncProcessResult> Run(AsyncProcessStartInfo startInfo)
产品 | 版本 兼容的以及额外的计算目标框架版本。 |
---|---|
.NET Framework | net461 是兼容的。 net462 已计算。 net463 已计算。 net47 已计算。 net471 已计算。 net472 已计算。 net48 已计算。 net481 已计算。 |
此包没有依赖。
NuGet 包
此包未由任何 NuGet 包使用。
GitHub 仓库
此包未由任何流行的 GitHub 仓库使用。
版本 | 下载 | 上次更新 |
---|---|---|
1.0.1905181338 | 732 | 5/18/2019 |
首次发布