How to execute perl script with input parameters and output the result to a file in C#
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. Plea开发者_运维百科se 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();
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.
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.
精彩评论