[ACCEPTED]-reading multiple times from same stream in c#-process

Accepted answer
Score: 11

If the stream supports seeking (CanSeek), you can 2 "rewind" it by setting

stream.Position = 0;

thus starting to 1 read it all over again.

Score: 3

If the stream does not support seeking but 4 the data in stream is not so big, you can 3 read and write that stream to MemoryStream, and 2 read from MemoryStream as many times as 1 you want.

Score: 0

Quck and Dirty: This one works with some 4 minor glitches. Try improving it, because 3 I'm leaving office :)

        ProcessStartInfo psi = new ProcessStartInfo(@"c:\temp\testC.exe");
        psi.CreateNoWindow = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;

        Process p = Process.Start(psi);
        string input = "";

        ConsoleColor fc = Console.ForegroundColor;

        StreamWriter sw = p.StandardInput;
        StreamReader sr = p.StandardOutput;

        char[] buffer = new char[1024];
        int l = 0;

        do
        {
            Console.Write("Enter input: ");
            input = Console.ReadLine();

            int i = Convert.ToInt32(input);

            sw.Write(i);
            sw.Write(sw.NewLine);

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.Write(">> ");

            l = sr.Read(buffer, 0, buffer.Length);

            for (int n = 0; n < l; n++)
                Console.Write(buffer[n] + " ");

            Console.WriteLine();

            Console.ForegroundColor = fc;
        } while (input != "10");

        Console.WriteLine("Excution Finished. Press Enter to close.");
        Console.ReadLine();
        p.Close();

PS:- I've created console 2 exe in vs2008 and copied it to c:\temp folder 1 under name testC.exe.

More Related questions