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.