使用 C# StandardInput 重定向时如何将 Ctrl+Enter 命令传递给 Process
How to pass Ctrl+Enter command to Process when using C# StandardInput redirection
我正在尝试制作使用 hunpos tagger 的 C# 应用程序。
运行 hunpos-tag.exe 需要三个输入参数:model、inputFile、outputFile
在 cmd 中它看起来像这样:
hunpos-tag.exe model <inputFile >outputFile
尽管 hunpos-tag.exe 可以只对模型 运行,此时它将等待文本输入(来自 cmd),当标记器收到 Ctrl+Enter 时处理作为输入,结果通过标准输出显示。我一直在尝试在 C# 中使用 StandardInput 重定向,但我不知道如何发送 Ctrl+Enter 结束命令(或者重定向是否有效)。代码:
string inputFilePath = path + "\CopyFolder\rr";
string pathToExe = path + "\CopyFolder\hunpos-tag.exe";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
RedirectStandardInput = true,
WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
Arguments = path + "\CopyFolder\model.hunpos.mte5.defnpout",
};
try
{
Process _proc = new Process();
_proc.StartInfo.FileName = pathToExe;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.Arguments = path + "\CopyFolder\model.hunpos.mte5.defnpout";
_proc.Start();
var streamReader = new StreamReader(inputFilePath);
_proc.StandardInput.Write(streamReader.ReadToEnd());
_proc.StandardInput.Flush();
_proc.StandardInput.Close();
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
当我 运行 以下代码时,标记器具有以下输出:
model loaded
tagger compiled
Fatal error: exception Sys_error("Bad file description")
异常是由.Close() 命令引起的。当从 cmd 运行ned 时,该文件是有效的并且可以工作。关于如何发送结束命令或如何在不使用重定向的情况下模拟 cmd 命令的任何想法?
它不适用于输入重定向,所以我用 运行 cmd procces 管理它并向它传递所需的命令。
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = @"C:\";
process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
// Runs the specified command and exits the shell immediately.
//process.StartInfo.Arguments = @"/c ""dir""";
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Send a directory command and an exit command to the shell
process.StandardInput.WriteLine(path + "\CopyFolder\hunpos-tag.exe " + path + "\CopyFolder\model.hunpos.mte5.defnpout <" + path + "\CopyFolder\rr >" + path + "\CopyFolder\zz");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
}
我正在尝试制作使用 hunpos tagger 的 C# 应用程序。 运行 hunpos-tag.exe 需要三个输入参数:model、inputFile、outputFile
在 cmd 中它看起来像这样:
hunpos-tag.exe model <inputFile >outputFile
尽管 hunpos-tag.exe 可以只对模型 运行,此时它将等待文本输入(来自 cmd),当标记器收到 Ctrl+Enter 时处理作为输入,结果通过标准输出显示。我一直在尝试在 C# 中使用 StandardInput 重定向,但我不知道如何发送 Ctrl+Enter 结束命令(或者重定向是否有效)。代码:
string inputFilePath = path + "\CopyFolder\rr";
string pathToExe = path + "\CopyFolder\hunpos-tag.exe";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
RedirectStandardInput = true,
WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
Arguments = path + "\CopyFolder\model.hunpos.mte5.defnpout",
};
try
{
Process _proc = new Process();
_proc.StartInfo.FileName = pathToExe;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.Arguments = path + "\CopyFolder\model.hunpos.mte5.defnpout";
_proc.Start();
var streamReader = new StreamReader(inputFilePath);
_proc.StandardInput.Write(streamReader.ReadToEnd());
_proc.StandardInput.Flush();
_proc.StandardInput.Close();
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
当我 运行 以下代码时,标记器具有以下输出:
model loaded
tagger compiled
Fatal error: exception Sys_error("Bad file description")
异常是由.Close() 命令引起的。当从 cmd 运行ned 时,该文件是有效的并且可以工作。关于如何发送结束命令或如何在不使用重定向的情况下模拟 cmd 命令的任何想法?
它不适用于输入重定向,所以我用 运行 cmd procces 管理它并向它传递所需的命令。
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = @"C:\";
process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
// Runs the specified command and exits the shell immediately.
//process.StartInfo.Arguments = @"/c ""dir""";
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Send a directory command and an exit command to the shell
process.StandardInput.WriteLine(path + "\CopyFolder\hunpos-tag.exe " + path + "\CopyFolder\model.hunpos.mte5.defnpout <" + path + "\CopyFolder\rr >" + path + "\CopyFolder\zz");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
}