簡體   English   中英

C#控制台編程如何在命令提示符下運行Perl腳本?

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

我想問幾個問題:

•如何執行命令“ 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#控制台程序中?

•如何顯示控制台的結果? 我應該使用“ console.writeline”嗎?

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

該命令在普通命令提示符下可以正常運行。 C#控制台程序執行時出現以下錯誤:

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

並且不會顯示任何結果。 執行該程序后,沒有生成“ 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 [day | [小時]文件:指定索引文件以及結果摘要

    -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:\\ 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”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM