从ncftpput.exe重定向标准/错误输出不稳定

本文关键字:错误 输出 不稳定 标准 ncftpput exe 重定向 | 更新日期: 2023-09-27 18:08:12

我正试图从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();
        }
    }
}

提前感谢!!

从ncftpput.exe重定向标准/错误输出不稳定

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