【问题标题】:C# console programming how to run perl script in command prompt?C#控制台编程如何在命令提示符下运行perl脚本?
【发布时间】:2010-11-15 07:09:21
【问题描述】:

我想问几个问题:

•如何执行命令“C:\strawberry\perl\bin\perl.exe C:\temp\bin\mactime.pl -b C:\temp\bin\testing.bodyfile -z UCT-8 > C# 控制台程序中的 C:\temp\bin\testing2.txt"?

•如何显示控制台的结果?我应该使用“console.writeline”吗?

mactime.pl 来自“The Sleuth Kit”窗口。

该命令在普通命令提示符下完美运行。 C# 控制台程序执行时出现以下错误:

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

并且不显示任何结果。程序执行后没有生成“testing2.txt”。

以下是我的代码:

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 参数:

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 [天 | hour] 文件:指定带有结果摘要的索引文件

    -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)

【问题讨论】:

    标签: c# perl command-prompt


    【解决方案1】:

    修正参数。在您的代码上,它目前看起来像:

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

    注意不匹配的"> 后面没有空格。 将其更改为:

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

    现在是这样的:

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

    【讨论】:

    • 对不起,我错过了结尾"。现已修复,请重试。 :)
    • 现在在程序控制台命令提示符处出现“无效日期:>”的错误......可能是由于正则表达式。我认为它的间距或“>”字符???
    • 所以错误来自 perl 脚本,对吧?日期的依据是什么?你是如何从 perl 中检索到这个的? ARGV[0]? ARGV[1]?等等。
    • 应该在普通 CMD 上运行的参数应该是这样的:"perl.exe mactime.pl -b text.bodyfile(bodyfile for mactime to read) -z UCT-8(timezone) text .txt(将结果计算成文本文件)”谢谢。
    • 所以我猜它所指的日期是时区吧?你能把代码放在你为变量分配参数的地方吗?
    猜你喜欢
    • 2014-03-18
    • 2013-03-27
    • 1970-01-01
    • 2016-11-21
    • 2020-03-03
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多