0

I have this method to write a line to a visual debug area in an ASP.NET application:

private void WriteDebug(string s)
{
    debugBox.Text = debugBox.Text + "\r\n" + s;
}

I call that method when running command line processes:

private void RunCommand(string command)
{
    WriteDebug("Command initiated\r\n\t" + command);
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo("cmd")
        {
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            Arguments = String.Format("/c \"{0}\"", command),
        }
    };
    process.OutputDataReceived += (s, e) => WriteDebug(e.Data);
    process.Start();
    process.BeginOutputReadLine();
}

Even though the commands are successful, debugBox never shows any of the output. I'm probably using OutputDataReceived wrong but I'm not sure how. Any help?

Cliff
  • 697
  • 8
  • 19
  • oops, editing the wrong comment. Use Environment.NewLine, also test WriteDebug("test") as a simpler case before relying on the process output. – payo Jun 05 '14 at 14:52
  • The `WriteDebug()` method works fine everywhere else. Uh, what's Environment.Newline for? – Cliff Jun 05 '14 at 14:58
  • 1
    have you tried using a breakpoint in WriteDebug to see if it ever gets process output callbacks? – payo Jun 05 '14 at 14:59
  • Example? I'm still learning C# so I'm a bit behind. – Cliff Jun 05 '14 at 15:00
  • http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx : "Gets the newline string defined for this environment." AFAIK, if you don't use this, your escaped carriage return and newline might not be appropriate for the environment your code is running in, and might not work. Also, see http://stackoverflow.com/questions/1015766/difference-between-n-and-environment-newline – Zack Jun 05 '14 at 15:04
  • Either via your ide (visual studio) see http://msdn.microsoft.com/en-us/library/5557y8b4%28v=vs.90%29.aspx (this is a page for vs 2008, but it works the same), you can also use System.Diagnostics.Debugger.Break(); – payo Jun 05 '14 at 15:04
  • The problem seems to be somewhere else. If I change `WriteDebug(e.Data)` to `Console.WriteLine(e.Data)`, the console doesn't write anything either. Even if I try a simple command like `help`. – Cliff Jun 05 '14 at 15:11

0 Answers0