I’m trying to run a perl script through C# code. The perl script takes a text file as input and creates two new files as output (doing some text processing in between). When I run the perl script through C# using the below code, it seems to start executing, but no files are created. Whats the problem? Syntax error?
ProcessStartInfo perlStartInfo = new ProcessStartInfo(@"c:\Perl\bin\perl.exe");
perlStartInfo.Arguments = "c:\\Perl\\DOC-MAT.pl " + "c:\\Perl\\comb1.txt" + "c:\\Perl\\comb1.mat";
perlStartInfo.UseShellExecute = false;
perlStartInfo.RedirectStandardOutput = true;
perlStartInfo.RedirectStandardError = true;
perlStartInfo.CreateNoWindow = false;
Process perl = new Process();
perl.StartInfo = perlStartInfo;
perl.Start();
perl.WaitForExit();
string output = perl.StandardOutput.ReadToEnd();
Here, comb1.txt is my input file, and comb1.mat and comb1.clabel should be the files getting created by the perl code.
It looks as though you are missing a space between the two file arguments in your string concatenation.
Should be:
Perhaps a better alternative is to build up an array or List of argument strings, and use
string.Jointo do the concatenation.The above will work in .NET4, previous versions may require you to call
args.ToArray()before passing it to the join method.Important edit: Also, I think you need to swap the last two lines to prevent the process waiting indefinitely for the output buffer to clear. See Process.StandardOutput (MSDN) for more details.