c# - 使用创建进程从 C# 运行 exe 时如何模仿标准输入?
问题描述
我有一个音频转换器 .exe,我想将它封装在 C# 程序中,用于 UI 和输入等。要使用 AudioConverter.exe,它是从带有后缀“< inputFile > ouputFile”的控制台运行的。所以整行读起来像
C:\\User\Audioconverter.exe < song.wav > song.ogg
到目前为止,我已经能够在 C# 之外成功启动转换器,我已经设法通过 C# 中的创建进程在挂起状态下运行转换器(没有输入和输出文件)。到目前为止,我在 C# 中的代码与此站点上给出的答案非常相似:
using System;
using System.Diagnostics;
namespace ConverterWrapper2
{
class Program
{
static void Main()
{
LaunchCommandLineApp();
}
static void LaunchCommandLineApp()
{
// For the example
const string ex1 = "C:\\Users\\AudioConverter.exe";
const string ex2 = "C:\\Users\\res\\song.wav";
const string ex3 = "C:\\Users\\out\\song.ogg";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "AudioConverter2.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = ex1 + " < " + ex2 + " > " + ex3; \\Process is ran successfully without the addition of input and output files, but hangs waiting for files.
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
}
}
到目前为止,转换器 exe 无法正确启动,这让我问这个问题是标准输入的输入与参数不同吗?
无论如何,我都需要模仿这种输入方式,并且会很感激任何信息。我曾假设我可以将输入和输出文件作为参数传递,但我运气不佳。
解决方案
startInfo.Arguments = ex1 + " < " + ex2 + " > " + ex3; \\Process is ran successfully without the addition of input and output files, but hangs waiting for files.
那是行不通的。
A.exe < B > C
不是用A.exe
参数调用的过程< B > C
。这是一个shell指令:
- 开始
A.exe
没有争论, - 读取文件
B
并将其内容重定向到新进程的标准输入和 - 将新进程的标准输出写入文件
C
。
在 C# 中有两种选择:
您可以使用 shell 的帮助,即,您可以从
cmd.exe
参数开始/c C:\User\Audioconverter.exe < song.wav > song.ogg
或您可以在 C# 中重新实现 shell 正在执行的操作。可以在这个相关问题中找到一个代码示例:
推荐阅读
- swift - 如何在附加到 UITextField 的 UIPickerView 上设置 selectRow
- mrtk - 是否可以在构建中强制使用 MRTK 的 InputSimulationService?
- react-select - 移除对MouseOut React-Select 的关注
- memory - FPGA Stratix 3 存储器能否处理大量数据?
- javascript - Javascript函数计算自以来经过的年数
- python - Pandas 中的条件累积和
- c++ - 如何调用存储在数组中的数组的值
- javascript - 赛普拉斯测试:如何比较 AJAX 调用前后的元素数量
- c++ - 如何使用 DirectX 11 渲染多个网格
- swift - 使用 ARKit 在 AR 中放置的 3D 对象中存储“标题”等数据