简体   繁体   English

C#控制台编程如何在命令提示符下运行Perl脚本?

[英]C# console programming how to run perl script in command prompt?

I would like to ask a few questions: 我想问几个问题:

•How to execute the command "C:\\strawberry\\perl\\bin\\perl.exe C:\\temp\\bin\\mactime.pl -b C:\\temp\\bin\\testing.bodyfile -z UCT-8 > C:\\temp\\bin\\testing2.txt" in a C# console program? •如何执行命令“ C:\\ strawberry \\ perl \\ bin \\ perl.exe C:\\ temp \\ bin \\ mactime.pl -b C:\\ temp \\ bin \\ testing.bodyfile -z UCT-8> C:\\ temp \\ bin \\ testing2.txt”在C#控制台程序中?

•How do I display the results of the console? •如何显示控制台的结果? Should I use "console.writeline"? 我应该使用“ console.writeline”吗?

The mactime.pl is from "The Sleuth Kit" windows. mactime.pl来自“ The Sleuth Kit”窗口。

The command works perfectly on the normal command prompt. 该命令在普通命令提示符下可以正常运行。 The C# console program executes with errors of: C#控制台程序执行时出现以下错误:

"Can't open C:\\temp\\bin\\testing.bodyfile -z UCT-8 >C:\\temp\\bin\\testing2.txt at C:\\temp\\bin\\mactime.pl line 282." “无法在C:\\ temp \\ bin \\ mactime.pl第282行打开C:\\ temp \\ bin \\ testing.bodyfile -z UCT-8> C:\\ temp \\ bin \\ testing2.txt。”

and does not disply any results. 并且不会显示任何结果。 There is no "testing2.txt" being generated after the program was executed. 执行该程序后,没有生成“ testing2.txt”。

The following are my codes: 以下是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
   static void Main(string[] args)
   {
       LaunchCommandLineApp(); 
   }   

   static void LaunchCommandLineApp()
{
    // For the example
    const string ex1 = "C:\\temp\\bin\\mactime.pl";
    const string ex2 = "C:\\temp\\bin\\testing.bodyfile";
    const string ex3 = "C:\\temp\\bin\\testing2.txt";

    // Use ProcessStartInfo class
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "C:\\strawberry\\perl\\bin\\perl.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "\"" + ex1 + "\" -b " + ex2 + "\" -z UCT-8 >" + ex3;

    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using statement will close.
        using (Process exeProcess  = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
  }
  }
  }

Mactime.pl Arguments: Mactime.pl参数:

mactime [-b body_file] [-p password_file] [-g group_file] [-i day|hour idx_file] [-d] [-h] [-V] [-y] [-z TIME_ZONE] [DATE] -b: Specifies the body file location, else STDIN is used -d: Output timeline and index file in comma delimited format -h: Display a header with session information -i [day | mactime [-b body_file] [-p password_file] [-g group_file] [-i day | hour idx_file] [-d] [-h] [-V] [-y] [-z TIME_ZONE] [DATE] -b :指定主体文件位置,否则使用STDIN -d:以逗号分隔格式输出时间轴和索引文件-h:显示带有会话信息的标头-i [day | hour] file: Specifies the index file with a summary of results [小时]文件:指定索引文件以及结果摘要

    -g: Specifies the group file location, else GIDs are used
    -p: Specifies the password file location, else UIDs are used
    -V: Prints the version to STDOUT
    -y: Dates have year first (yyyy/mm/dd) instead of (mm/dd/yyyy)
    -m: Dates have month as number instead of word (can be used with -y)
    -z: Specify the timezone the data came from (in the local system format)

    [DATE]: starting date (yyyy-mm-dd) or range (yyyy-mm-dd..yyyy-mm-dd)

Fix the arguments. 修正参数。 On your code it currently looks like: 在您的代码上,当前看起来像:

"C:\\temp\\bin\\mactime.pl" -b C:\\temp\\bin\\testing.bodyfile" -z UCT-8 >C:\\temp\\bin\\testing2.txt “ C:\\ temp \\ bin \\ mactime.pl” -b C:\\ temp \\ bin \\ testing.bodyfile“ -z UCT-8> C:\\ temp \\ bin \\ testing2.txt

Notice the unmatched " and there's no space after > . Change that into: 注意不匹配的">之后没有空格。将其更改为:

startInfo.Arguments = "\"" + ex1 + "\" -b \"" + ex2 + "\" -z UCT-8 > \"" + ex3 + "\"";

Its now like: 现在像:

"C:\\temp\\bin\\mactime.pl" -b "C:\\temp\\bin\\testing.bodyfile" -z UCT-8 > "C:\\temp\\bin\\testing2.txt" “ C:\\ temp \\ bin \\ mactime.pl” -b“ C:\\ temp \\ bin \\ testing.bodyfile” -z UCT-8>“ C:\\ temp \\ bin \\ testing2.txt”

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

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