locked
process.StandardOutput retrieving only partial data RRS feed

  • Question

  • I am trying to run an exe using process in the c# console application and getting the output written line by line to a TXT file. This is supposed to recursively run across all the sub folder files.

    This perfectly works when I run with the folders having less files. But when this is run at the top level folders having huge list of files or at the drive level (like D:\ or E:\ ) this doesn't provide the whole output and it stops in between saying its complete. This happens the same with console output as well.

    Tried to enable Standarderror and output that as well, but was not able to get any errors though. Please suggest why this would complete with partial data and doesn't run through the whole list?

    Process sigCheckProcess = new Process();
    sigCheckProcess.StartInfo.FileName = sigcheckdir + "\\sigcheck64.exe";
    sigCheckProcess.StartInfo.Arguments = "-a -h -i -l -s " + dir;
    sigCheckProcess.StartInfo.RedirectStandardOutput = true;
    sigCheckProcess.StartInfo.UseShellExecute = false;
    sigCheckProcess.StartInfo.CreateNoWindow = true;
    sigCheckProcess.Start();
    
    using (StreamWriter streamWriter = File.CreateText(@"D:\Sigcheckoutput.txt"))
    {
        using (StreamReader streamReader = sigCheckProcess.StandardOutput)
        {
            while (!streamReader.EndOfStream)
            {
                var content = sigCheckProcess.StandardOutput.ReadLine();
                streamWriter.WriteLine(content);
            }
        }
    }
    sigCheckProcess.WaitForExit();

    Monday, December 17, 2018 9:30 AM

All replies

  • Unfortunately that isn't how you read the process output. Refer to the example in MSDN that demonstrates how to properly read the output of a command line call so you don't lose anything. It uses the async approach which is preferred.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, December 17, 2018 2:56 PM
  • The issue here is that your external process will flush data to its standard output in blocks, as its buffers fill.  StreamReader.EndOfStream will return true when the current block is finished, even though the app will eventually write more.

    You can work around that by using ReadLine() instead.  It will block for new input, and return Null when the stream is closed on the other end.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, December 17, 2018 7:12 PM
  • @Tim Roberts: I tried to use this, but doesnt log any output. Can you please suggest if I made any mistake in my code?

    using (StreamWriter streamWriter = File.CreateText(@"D:\Sigcheckoutput.txt"))
    {
        using (StreamReader streamReader = sigCheckProcess.StandardOutput)
        {
             var content = sigCheckProcess.StandardOutput.ReadLine();
             streamWriter.WriteLine(content);
        }
    }


    Monday, December 17, 2018 8:29 PM
  • Thanks @CoolDadTx! I will try this method as well and see if that works for me.
    Monday, December 17, 2018 8:29 PM
  • Hi dhilip gopalan,

    Is there any update, does the method resolve your issue, if you have any issue, please feel free let us know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 8:57 AM
  • Hi Zhanglong,

    I was not able to find the exact solution as this seems to be issue with the buffer. So I had to find a work around for this issue by collecting all the file paths in the directory and execute it one by one in a loop which seems to work as expected now.


    • Edited by dhilip gopalan Friday, December 28, 2018 1:41 AM updated body
    Friday, December 28, 2018 1:41 AM