2013-04-09 77 views
0

我试图从ncftpput.exe获取输出流,并能够异步操纵流。 ncftpput.exe不会逐行打印它的流,而只是用新信息更新相同的行(这可能是相关的)。它间歇地工作 - 有时我会得到信息,有时候我不会。从ncftpput.exe不稳定的重定向标准/错误输出

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

这里是我的本钱到目前为止(我注释它,摆脱irrelvant无关的信息,但是这是本质):提前

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(); 
     } 
    } 
} 

谢谢!

回答

0

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