【问题标题】:How to pass Ctrl+Enter command to Process when using C# StandardInput redirection使用 C# StandardInput 重定向时如何将 Ctrl+Enter 命令传递给 Process
【发布时间】:2015-04-13 03:08:42
【问题描述】:

我正在尝试制作使用 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 运行时有效。关于如何发送结束命令或如何在不使用重定向的情况下模拟 cmd 命令的任何想法?

【问题讨论】:

    标签: c# process cmd command-line-arguments pos-tagger


    【解决方案1】:

    它不适用于输入重定向,所以我通过运行 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();
            }
    

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多