繁体   English   中英

从C#服务调用Perl脚本

[英]Calling Perl Script from C# Service

我有一个ac#服务,需要调用perl脚本。 现在,我在桌面上有perl脚本,只引用了桌面的完整路径。 是否可以将perl脚本添加到c#项目中,并将其构建到与构建后生成的.exe相同的目录中? 这样,可以从当前路径引用文件。 代码如下。 另外,perl脚本使用了脚本中不需要的敏感信息,最好的方法是将敏感信息作为参数通过c#传递吗?

谢谢,

ProcessStartInfo perlStartInfo =新的ProcessStartInfo(@“ C:\\ strawberry \\ perl \\ bin \\ perl.exe”); perlStartInfo.RedirectStandardInput = true; perlStartInfo.UseShellExecute = false; perlStartInfo.CreateNoWindow = true; 流程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();

我添加了perl.Start(); 代码。 我一直到perl.WaitForExit(); 但它只是挂在那里。 任何想法?

如果您不想将.pl文件持久保存到磁盘,则可以启动perl.exe并通过STDIN从资源流中通过管道传递脚本。 将perl文件添加到您的项目中,将构建操作设置为“ Resource”,然后使用以下命令开始该过程:

// 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM