Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将控制台输出重定向到WPF应用程序的文本框时出错_C#_Wpf - Fatal编程技术网

C# 将控制台输出重定向到WPF应用程序的文本框时出错

C# 将控制台输出重定向到WPF应用程序的文本框时出错,c#,wpf,C#,Wpf,我正在尝试将控制台输出重定向到我的WPF应用程序文本框。 但我正在变得异常 "The system connot find the file specified" 这是我的密码 { string rootDir = sourcePath; string command = CAConstants.CPPCheckCommand + sourcePath + " 2> " + rootDir + CAConstants.FileSeparator + CAConstant

我正在尝试将控制台输出重定向到我的WPF应用程序文本框。 但我正在变得异常

"The system connot find the file specified"
这是我的密码

{
    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();
    }
}

我找到了这个链接

但无法解决我的错误

这是我的命令

cppcheck -v --enable=all --xml C:\\Test_projects\\52 2> C:\\Test_projects\\52\\cppcheck.xml
启动进程时出错

当我在命令提示符下运行它时,它工作得很好。 我错过了什么?
任何帮助。

要重定向Visual Studio中控制台的进程输出,只需将设置为
true
,将设置为
false

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
从链接页面:

如果要将RedirectStandardOutput设置为true,则必须将UseShellExecute设置为false。否则,从StandardOutput流读取将引发异常

现在,您应该能够从中访问
StreamReader
。从上面的链接页面:

重定向的标准输出流可以同步或异步读取。Read、ReadLine和ReadToEnd等方法对进程的输出流执行同步读取操作。这些同步读取操作只有在关联进程写入其标准输出流或关闭该流时才能完成

相反,BeginOutputReadLine在标准输出流上启动异步读取操作。此方法为流输出启用指定的事件处理程序,并立即返回调用方,调用方可以在流输出定向到事件处理程序时执行其他工作


但是,我可以看到您已经在这样做了,所以我只能建议您有其他一些导致错误的代码。如果它说系统找不到指定的文件,则很可能是您的一个或多个文件路径无效。

首先猜测是
pathVar
cppcheckPath
没有指向实际文件。他们的价值观是什么?错误发生在哪一行?此外,您应该使用
System.IO.Path.Combine
连接路径信息。它将为操作系统添加适当的分隔符,您不必担心斜杠丢失或转义。
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;