Search code examples
c#.netstdin

"StandardIn has not been redirected" error in .NET (C#)


I want to do a simple app using stdin. I want to create a list in one program and print it in another. I came up with the below.

I have no idea if app2 works however in app1 I get the exception "StandardIn has not been redirected." on writeline (inside the foreach statement). How do I do what I intend?

NOTE: I tried setting UseShellExecute to both true and false. Both cause this exception.

        //app1
        {
            var p = new Process();
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.FileName = @"path\bin\Debug\print_out_test.exe";
            foreach(var v in lsStatic){
                p.StandardInput.WriteLine(v);
            }
            p.StandardInput.Close();
        }

    //app 2
    static void Main(string[] args)
    {
        var r = new StreamReader(Console.OpenStandardInput());
        var sz = r.ReadToEnd();
        Console.WriteLine(sz);
    }

Solution

  • You never Start() the new process.