Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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:从shell中执行的脚本读取日志?_C#_.net_Wpf - Fatal编程技术网

C# WPF:从shell中执行的脚本读取日志?

C# WPF:从shell中执行的脚本读取日志?,c#,.net,wpf,C#,.net,Wpf,我使用以下代码在命令提示符下启动外部程序: private void GenerateTiff(String fileName) { bool success = true; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { try

我使用以下代码在命令提示符下启动外部程序:

private void GenerateTiff(String fileName) {
            bool success = true;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += delegate
            {
                try
                {
                    String cmd = @"./lib/gswin32c";
                    String args = "-dNOPAUSE -sDEVICE=pngalpha -r300 -dBATCH -dSAFER -sOutputFile=" + fileName + "-%03d" + FILEEXTENSION + " " + fileName + ".pdf";
                    Process proc = new Process();
                    proc.StartInfo.FileName = cmd;
                    proc.StartInfo.Arguments = args;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.UseShellExecute = false;
                    proc.Start();
                }
                catch (Exception e)
                {
                    success = false;
                }
            };
            worker.RunWorkerCompleted += delegate
            {
                string file = fileName + "-001.jpg";

                if (success) {
                    DisplayImage.Visibility = System.Windows.Visibility.Visible;
                    DisplayImage.Tag = fileName;
                }
            };
            worker.RunWorkerAsync();
        }
现在我想阅读命令提示符的日志。我该怎么做呢?

看看酒店。它为您提供了一个StreamReader,可用于读取命令的输出

但是,请务必阅读文档,因为有些情况需要注意,例如:

要使用StandardOutput,必须将ProcessStartInfo.UseShellExecute设置为false,并且必须将ProcessStartInfo.RedirectStandardOutput设置为true。否则,从StandardOutput流读取将引发异常


如果您的外部程序写入stderr而不是stdout,请改用。

一个:另一个:您的函数名为GenerateTiff,在文件名中添加-001.*jpg*,传递给GhostScript的参数表明您实际上正在创建png文件。您确定已整理好文件格式吗?是;)这是因为我首先创建了png。然后我创建了TIFF,然后我尝试使用jpeg。但你是对的。。。这就是重构它的原因。