0

I want to execute a perl script in my c# code and output the result to a file. The perl script will take in a binary file name as an input parameter, and I want to redirect the result to a text file. I have the following in my c# code, but the output file test.txt is not created. Please advise if you know the problem:

private Process myProcess = null;

myProcess = new Process();

myProcess.StartInfo.FileName = "perl.exe";
myProcess.StartInfo.Arguments = "C:\\mydir\\myPerl.pl C:\\mydir\\myFile.DAT > C:\\mydir\\test.txt";
myProcess.Start();
joce
  • 9,624
  • 19
  • 56
  • 74
CSharpTag
  • 1
  • 1
  • Do you have any evidence that the Perl script runs and works correctly? If so, I suspect that this is really a C# issue. Otherwise I'd validate that the Perl script is correct first. – David Harris May 03 '11 at 18:34
  • The perl script is correct because if I execute it using the command line, it works. – CSharpTag May 03 '11 at 18:42

2 Answers2

1

I've answer a similar question a couple of times before:

Here's my previous answer. Just replace the delegates to write to a file instead.

ProcessStartInfo processInfo = new ProcessStartInfo("Myexe.exe");
processInfo.ErrorDialog = false;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;

Process proc = Process.Start(processInfo);

// You can pass any delegate that matches the appropriate 
// signature to ErrorDataReceived and OutputDataReceived
proc.ErrorDataReceived += (sender, errorLine) => { if (errorLine.Data != null) Trace.WriteLine(errorLine.Data); };
proc.OutputDataReceived += (sender, outputLine) => { if (outputLine.Data != null) Trace.WriteLine(outputLine.Data); };
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();

proc.WaitForExit();

In your particular case, don't forget to remove the "> C:\\mydir\\test.txt" from your command line.

Community
  • 1
  • 1
joce
  • 9,624
  • 19
  • 56
  • 74
0

Does the program execute? I mean is it just that the output file doesn't get created or the process is not starting? By the way, you will need to escape those backslashes or use @ before the argument string.

The redirection is not an argument, so I don't think you can specify "> C:\mydir\test.txt"" in the arguments parameter. Try using Process.StandardOutput for this instead. Or you can also try taking in the output file as an argument in the perl script and make the perl code write the text file.

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
  • Yes, the program execute without any problem, but the output is not created. – CSharpTag May 03 '11 at 18:41
  • Tried Process.StandardOutput (see the examples on the page, you need to set Process.StartInfo.UseShellExecute = false and Process.StartInfo.RedirectStandardOutput = true before using it.) – Hari Menon May 03 '11 at 18:47