【问题标题】:Erratic redirecting standard/error output from ncftpput.exe从 ncftpput.exe 重定向标准/错误输出不稳定
【发布时间】:2013-04-01 07:32:07
【问题描述】:

我正在尝试从 ncftpput.exe 获取输出流并能够异步操作流。 ncftpput.exe 不会逐行打印它的流,而是不断用新信息更新同一行(这可能是相关的)。它间歇性地工作 - 有时我会得到信息,有时我不会。

基本上,有没有一种方法可以让流频繁刷新它的行,以便以更安全和更常规的方式重定向?

这是我到目前为止所得到的(我已经对其进行了注释,消除了不相关的无关信息,但这就是本质):

class Program
{
    static int main()
    {
        internal Process m_Tool { get; set; }
        internal ProcessStartInfo m_StartInfo { get; set; }
        internal static string m_StandardData { get; set; }
        internal static string m_ErrorData { get; set; }

        m_Tool = new Process();
        m_StartInfo = new ProcessStartInfo();
        m_StartInfo.FileName = @"C:\utils\ncftpput.exe";
        m_StartInfo.UseShellExecute = false;
        m_StartInfo.RedirectStandardOutput = true;
        m_StartInfo.RedirectStandardError = true;
        m_StandardData = "";
        m_ErrorData = "";
        m_StartInfo.Arguments = /* Various starting args */
        m_Tool.StartInfo = m_StartInfo;
        m_Tool.Start();

        string standardLine;
        string errorLine;

        try
        {
            m_Tool.OutputDataReceived += ProcessStandardDataHandler;
            m_Tool.ErrorDataReceived += ProcessErrorDataHandler;

            m_Tool.BeginErrorReadLine();
            m_Tool.BeginOutputReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        while (!m_Tool.HasExited)
        {
            System.Threading.Thread.Sleep(5000);
            standardLine = m_StandardData;
            errorLine = m_ErrorData;
        } 
    }
    private static void ProcessErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {

        if (!String.IsNullOrEmpty(outLine.Data))
        {
            // Add the text to the collected output.
            m_ErrorData = outLine.Data.ToString();
            m_DataReceived = true;

        }
    }

    private static void ProcessStandardDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {

        if (!String.IsNullOrEmpty(outLine.Data))
        {
            // Add the text to the collected output.
            m_StandardData = outLine.Data.ToString();
        }
    }
}

提前致谢!!

【问题讨论】:

    标签: c# process stream redirectstandardoutput


    【解决方案1】:

    最后我发现由于 ncftpput.exe 打印到控制台的方式,从重定向的输出中获取足够的信息是不可能的。所以我决定编写自己的 FTP 应用程序并使用它!这是一个比 ncftpput.exe 更简单的应用程序,但它可以满足我的需要。我用的是标准的 .NET 库,没什么特别的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2011-10-11
      • 1970-01-01
      • 2012-03-30
      • 2012-05-26
      • 2019-11-08
      • 2017-05-13
      相关资源
      最近更新 更多