-1

I am trying to redirect the console output to my WPF application TextBox. But I am getting below exception

"The system connot find the file specified"

Here is my code

{
    string rootDir = sourcePath; 
    string command = CAConstants.CPPCheckCommand + sourcePath + " 2> " + rootDir + CAConstants.FileSeparator + CAConstants.CPPCHECKFileName;

    using (proc = new Process())
    {
        // set environment variables
        string pathVar = proc.StartInfo.EnvironmentVariables[CAConstants.ENV_Path];
        string cppcheckPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) +  CAConstants.CPPCheckRootDir;
        proc.StartInfo.EnvironmentVariables[CAConstants.ENV_Path] = pathVar + cppcheckPath + ";";

        //set process name
        proc.StartInfo.FileName = command;

        proc.StartInfo.UseShellExecute = false;

        // set up output redirection
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.EnableRaisingEvents = true;
        proc.StartInfo.CreateNoWindow = true;

        // see below for output handler
        proc.ErrorDataReceived += proc_DataReceived2;
        proc.OutputDataReceived += proc_DataReceived2;

        proc.Start();   // Getting error at this line

        proc.BeginErrorReadLine();
        proc.BeginOutputReadLine();

        //proc.WaitForExit();
    }
}

and

void proc_DataReceived2(object sender, DataReceivedEventArgs e)
{
    // output will be in string e.Data
    if (!String.IsNullOrEmpty(e.Data))
    {
        if (!logsTextBox.Dispatcher.CheckAccess())
        {
            // Called from a none ui thread, so use dispatcher
            ShowLoggingDelegate showLoggingDelegate = new ShowLoggingDelegate(ShowLogging);
            logsTextBox.Dispatcher.Invoke(DispatcherPriority.Normal, showLoggingDelegate, e.Data);
        }
        else
        {
            // Called from UI trhead so just update the textbox
            ShowLogging(e.Data);
        };
    }
}

private delegate void ShowLoggingDelegate(string text);

private void ShowLogging(string text)
{
    logsTextBox.AppendText(text);
    logsTextBox.ScrollToEnd();
}

I found this links, Redirect console output to textbox in separate program

but not able to resolve my error

this is my command

cppcheck -v --enable=all --xml C:\\Test_projects\\52 2> C:\\Test_projects\\52\\cppcheck.xml

getting error when I am starting process.

and when I run this on command prompt, its working fine. what I am missing? Any help.

Community
  • 1
  • 1
atulya
  • 535
  • 2
  • 8
  • 25
  • First guess would be that `pathVar` and `cppcheckPath` dont point to an actually file. What are their values? And on what line does your error occur? – crthompson Jun 16 '14 at 05:02
  • Additionally, you should use `System.IO.Path.Combine` to concatenate path information. It will add the proper separator for the OS and you dont have to worry about missing or escaping slashes. – crthompson Jun 16 '14 at 05:05

1 Answers1

1

All you need to do to redirect the process output from the Console in Visual Studio is to set the ProcessStartInfo.RedirectStandardOutput property to true and the ProcessStartInfo.UseShellExecute property to false:

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;

From the linked page:

You must set UseShellExecute to false if you want to set RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception.

Now you should be able to access the StreamReader from the Process.StandardOutput Property. From the above linked page:

The redirected StandardOutput stream can be read synchronously or asynchronously. Methods such as Read, ReadLine, and ReadToEnd perform synchronous read operations on the output stream of the process. These synchronous read operations do not complete until the associated Process writes to its StandardOutput stream, or closes the stream.

In contrast, BeginOutputReadLine starts asynchronous read operations on the StandardOutput stream. This method enables a designated event handler for the stream output and immediately returns to the caller, which can perform other work while the stream output is directed to the event handler.

However, I can see that you are already doing this, so I can only suggest that you have some other code that is causing your error. If it says The system cannot find the file specified, then it is most likely that one or more of your file path(s) are invalid.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183