Why is Process.StandardInput.WriteLine not supplying input to my process?

前端 未结 2 1962
不思量自难忘°
不思量自难忘° 2020-12-21 02:57

this is my first question here so I\'ll be as detailed as I can. I am currently working on a C# program (We\'ll call it TestProgram) that tests a different program written i

2条回答
  •  Happy的楠姐
    2020-12-21 03:33

    Your C# code seems to be just fine, I tried it with another C# program:

    static void Main(string[] args)
    {
        String line = Console.ReadLine();
        Console.WriteLine("I received " + line);
    }
    

    And the following code outputs "I received Hello world!". So the problem must be in your C code. As dasblinkenlight already mentioned, new line symbols might be missing.

    static void Main(string[] args)
    {
        Process cmd = new Process();
    
        ProcessStartInfo info = new ProcessStartInfo(@"AnotherApp.exe", "");
        info.WorkingDirectory = @"path\to\folder";
        info.RedirectStandardInput = true;
        info.RedirectStandardOutput = true;
        info.RedirectStandardError = true;
        info.UseShellExecute = false;
        cmd.StartInfo = info;
        cmd.Start();
    
        cmd.StandardInput.WriteLine("Hello world!");
    
        String output = cmd.StandardOutput.ReadLine();
        Console.WriteLine(output);
    
        Console.ReadKey();
    }
    

提交回复
热议问题