views:

216

answers:

2

Hello: I am trying to get the following code to work so I can call a perl script from my c# program. I am developing using visual stdio 2008 on xp service pack3.

 myProcess = new Process();
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
        myProcessStartInfo.Arguments = @"C:\Documents and Settings\test_perl.pl";
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        myProcessStartInfo.CreateNoWindow = true;
        myProcess.StartInfo = myProcessStartInfo;

        myProcess.Start();
        string output = myProcess.StandardOutput.ReadToEnd();
        MessageBox.Show(output);
        myProcess.WaitForExit();

I verify the test_perl.pl exists and If I change the perl.exe to notepad.exe, thie above code works. But if I use perl.exe, the message box is empty.

Can't figure out why this is wrong. Please help me if you know why.

Thanks

A: 

Is perl in your %PATH%? Open a command prompt and type in "perl -v"

tster
Yes, I can execute perl xxx.pl or xxx.txt on dos window.
alex
I'm confused, you marked this as correct but it seems it's still not working for you. I just tried this and it worked for me. Are you sure the perl program prints anything? Are you sure that it prints on standard out? Try it in a console program instead of a windows application. what is the content of the perl program?
tster
+4  A: 

Can perl.exe handle unquoted paths containing spaces on the command line? Try quoting the path:

myProcessStartInfo.Arguments = @"""C:\Documents and Settings\test_perl.pl""";

Since command-line arguments are delimited by spaces, unless the file path is quoted, the application (perl.exe, in this case) will see three arguments:

  1. C:\Documents
  2. and
  3. Settings\test_perl.pl

Perl will likely try to open the file "C:\Documents". This doesn't exist, of course. The solution is to quote file paths that contain spaces (or all file paths, to be consistent).

You mention that notepad.exe handles unquoted file paths fine. Likely, that's just notepad being smarter than the average bear, and merging its arguments for you.

And verify that a file exists at that path, of course. That's actually a slightly unusual path; normally, you'll see user files in something like C:\Documents and Settings\myusername\Documents\file.ext, or such.

Michael Petrotta
I don't think there is anything wrong with the file path since if I change perl.exe to notepad.exe, it works. I also get rid of the myusername\Documents\ on purpose.
alex
...try it anyway. notepad != perl.
Michael Petrotta
Michael, you are good! Thanks! It is working now. But Why the extra "" there?
alex
also, thanks tster for the help!
alex
You should change the check to this answer.
tster
@alex: glad it helped. Since double-quotes are used to delimit strings, you need to escape the quotes if you want to include them **inside** a string. In a standard string, you would use a backslash (\") for this. Here, you're marking the string as a verbatim string with the "@" symbol. To escape double-quotes in verbatim strings, double them ("").
Michael Petrotta
See http://msdn.microsoft.com/en-us/library/362314fe%28VS.71%29.aspx for more information.
Michael Petrotta