使用C#进行Perl脚本输出捕获问题

时间:2009-12-02 03:07:00

标签: c# perl

我正在关注其中一个线程来运行我的c#程序中的perl脚本。

我的c#代码是这样的:

   private void RunScript(ArrayList selectedScriptFileList)
    {
        foreach (var curScriptFileName in selectedScriptFileList)
        {
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
            myProcessStartInfo.Arguments = (string)(curScriptFileName);
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcessStartInfo.CreateNoWindow = true;
            myProcess.StartInfo = myProcessStartInfo;


            myProcess.Start();
            myProcess.WaitForExit();
            string output = myProcess.StandardOutput.ReadToEnd();
            this.ScriptTestResultTextBox.AppendText(output);
        }            
    }

我的perl脚本需要XML解析。我可以在XML解析之前读取print语句,但在解析开始之后不能读取。该脚本在DoS shell上运行find。

以下是我的脚本的一部分:

print("\n");
print("****************** test1.pl ***********************\n");
print("\n");

print("1");
print("2");

my $scriptName = 'test1.pl';
my $file = '../../ScriptParamLib.xml';
my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($file);
my $root = $tree->getDocumentElement;
my @species = $root->getElementsByTagName('test_node');

print("Accessing XML Data Base...\n");

c#testbox仅显示前三个print语句,但不显示最后一个。 有谁知道为什么?

由于

2 个答案:

答案 0 :(得分:4)

您可以添加更多调试打印语句(例如,代码的每个其他行之间的一个),以查看执行的程度。但是,我将采取预感并建议将这三行添加到您的脚本中,或者直接解决问题,或者让您更接近解决方案:

use strict;
use warnings;
use XML::LibXML;

请更新您的问题,说明执行的程度以及您看到的错误!

答案 1 :(得分:0)

我认为我应该将我的评论写成答案,因为它们证明是有用的:

由于在Perl脚本中使用$file的绝对路径可行,因此该问题很可能与从C#程序生成的进程的工作目录有关。您可以使用Perl脚本中的Cwd module来查看工作目录的实际内容。如果它不是您所期望的,请尝试通过C#程序中ProcessStartInfo的WorkingDirectory属性进行设置。在此之后,相对路径应该可以正常工作。

相关问题