管道传递命令行输入 C# 版

之前有探索过 Python 命令行程序 pipe:https://yanwen.blog/2018/06/15/利用管道传递命令行输入(stdin)/。使用 Python subprocess 库即可实现。

在实现过程中,可以选择 stdin 是否 line_buffering,如果 bufferring,则可以做到即时输入输出。

此处选择记录的还是之前探索过的内容,不过是 C# 实现,且为一个类似命令行代理的程序。

代理程序 PipeIPC:

using System;
using System.Diagnostics;
using System.IO;
using System.Text;

/**
 * Communicate with other command line program
 * 
 *
 * @ref https://www.techcoil.com/blog/how-to-interact-with-applications-via-command-line-in-c/
 * @ref https://stackoverflow.com/questions/49259695/_process-standardoutput-read-does-not-get-data-reliably-and-_process-standardinput
 *
 * @example new PipeIPC("E:\Wen Yan\Desktop\dev\cmd\WriteBack\WriteBack\bin\Release\WriteBack.exe").Pipe();
 */

namespace IPC
{
    class PipeIPC
    {
        private string _programName = "";

        private Process _process;

        public PipeIPC(string programName)
        {
            _programName = programName;

            _process = new Process
            {
                EnableRaisingEvents = true,
                StartInfo = new ProcessStartInfo
                {
                    FileName = @_programName,
                    Arguments = string.Empty,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    WorkingDirectory = Directory.GetCurrentDirectory(),
                    StandardOutputEncoding = Encoding.UTF8,
                    StandardErrorEncoding = Encoding.UTF8,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                }
            };

            _process.OutputDataReceived += (s, e) => // instead of using a background worker
            {
                if (e.Data == null) return;
                Console.WriteLine(e.Data);
            };
        }

        public void Pipe()
        {
            Console.WriteLine("Starting subprocess...");
            if (!_process.Start()) return;

            // Begin pipe
            _process.BeginOutputReadLine();  // XXX!

            StreamWriter inputWriter = _process.StandardInput;
            inputWriter.AutoFlush = true;

            string input;
            while ((input = Console.ReadLine()) != null)
                inputWriter.WriteLine(input);

            Console.WriteLine("Done");

            // Exit subproces
            _process.Kill();
        }
    }
}

回显程序 WriteBack:

for (string line; (line = Console.ReadLine()) != null; Console.WriteLine(line)) ;

参考:

  1. https://www.techcoil.com/blog/how-to-interact-with-applications-via-command-line-in-c/
  2. https://stackoverflow.com/questions/49259695/process-standardoutput-read-does-not-get-data-reliably-and-process-standardinput

作者: V

Web Dev

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s