Pregunta

I'm trying to get the output stream from ncftpput.exe and be able to manipulate the stream asynchronously. ncftpput.exe doesn't print it's stream line by line, instead it just keeps updating the same line with new information (this might be relevant). It works intermittantly - sometimes I'll get the information back, sometimes I don't.

Essentially, is there a way I can get a stream that refreshes it's line frequently to be redirected in a more secure and regular manner?

Here is what I've got so far (I've annotated it, getting rid of irrelvant extraneous information, but this is the essence):

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

Thanks in advance!!

¿Fue útil?

Solución

In the end I discovered that because of how ncftpput.exe printed to the console, it was going to be impossible to get sufficient information from the redirected output. So I just decided to write my own FTP application and use that! It's a simpler application than ncftpput.exe but it does what I need it to do. I used the standard .NET library, nothing special.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top