0
votes

I have a c# service that needs to call a perl script. Right now I have the perl script on my desktop and just reference the complete path of the desktop. Is it possible to add the perl script to the c# project and have it build to the same directory as the .exe that is generated after building? That way the file can be referenced from the current path. Code is below. Also the perl script uses sensitive information that I don't want on the script, would the best way to do this be passing the sensitive information as a parameter through c#?

Thanks,

ProcessStartInfo perlStartInfo = new ProcessStartInfo(@"C:\strawberry\perl\bin\perl.exe"); perlStartInfo.RedirectStandardInput = true; perlStartInfo.UseShellExecute = false; perlStartInfo.CreateNoWindow = true; Process perl = new Process(); perl.StartInfo = perlStartInfo; perl.Start();

        byte[] byteArray = Encoding.ASCII.GetBytes(Properties.Resources.TransferChange);

        using (MemoryStream stream = new MemoryStream(byteArray))
        {

            stream.CopyTo(perl.StandardInput.BaseStream);

            // this will cause perl to execute the script
            perl.StandardInput.Close();
        }
        perl.Start();
        perl.WaitForExit();
        string output = perl.StandardOutput.ReadToEnd();

I added perl.Start(); to the code. I get all the way to perl.WaitForExit(); but it just hangs there. Any idea?

1
Add it to project and set to copy during build... Is it what you need? - Alexei Levenkov

1 Answers

1
votes

If you want to never persist the .pl file to disk, you could start perl.exe and pipe the script in via STDIN from a resource stream. Add the perl file to your project, set build action to be "Resource" then use the below to start the process:

// set up processstartinfo 
perlStartInfo.RedirectStandardInput = true;
Process perl = new Process();
perl.StartInfo = perlStartInfo;
perl.Start();

using (var scriptStream = typeof(ThisClassType).Assembly.GetResourceStream(new Uri("thescript.pl")).Stream)
{
    scriptStream.CopyTo(perl.StandardInput.BaseStream);
    // this will cause perl to execute the script
    perl.StandardInput.Close();
}

perl.WaitForExit();
string output = perl.StandardOutput.ReadToEnd();