2012-06-06 104 views
0

我有1個c#console appln,它使用Process.Start()方法執行任何腳本文件。我提供的腳本文件路徑爲Process1.StartInfo.FileName。我的腳本文件可以是任何類型(.vbs,ps1等)。我也通過使用指令p.StartInfo.Arguments將String傳遞給腳本。當執行腳本文件時,它應該將字符串重新回到c#應用程序。返回的字符串可以通過設置Process1.StartInfo.RedirectStandardOutput = true來讀取,但是對於使用這條指令,我需要設置Process1.StartInfo.UseShellExecute = false。 當我運行這個我得到錯誤爲「指定的可執行文件不是有效的Win32應用程序」。 我想這可能是因爲,當我設置Process1.StartInfo.UseShellExecute = false時,我的appln不知道用哪個.exe來執行腳本文件。Process.StartInfo.UseShellExecute相關錯誤

另一方面,如果我提供的exe路徑StartInfo.FileName和腳本文件路徑StartInfo.Argument然後我沒有得到錯誤。 例如:我想執行PowerShell腳本,我設置以下屬性爲P1.StartInfo.FileName = "location of powershell.exe"p1.startInfo.Argument =".ps1 script file path",在這種情況下我沒有得到錯誤。

問題是我不提前知道哪種類型的腳本我要執行。也不能找出執行腳本文件在不同的不同的m/c上的.exe文件的位置。那麼是否有可能從相同的通用c#appln執行不同類型的腳本文件,並讀取腳本返回的輸出?

這裏是我的代碼

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Diagnostics; 
using System.Collections; 
namespace process_csharp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     String path = null; 
     //this will read script file path 
     path = Console.ReadLine(); 

     //this string is passed as argument to script 
     string s = "xyz"; 

     Process p = new Process();   
     p.StartInfo.FileName= path;   
     p.StartInfo.Arguments = s;      
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true;  

     p.Start(); 
     p.BeginOutputReadLine(); 
     Console.WriteLine(p.StandardOutput.ReadToEnd()); 
     } 

    } 
    } 

回答

2

您可以檢查腳本類型並從自己的引擎讀取輸出;

static void Main(string[] args) 
    { 
     string path = Console.ReadLine(); 
     string parameter = Console.ReadLine(); 
     string enginePath; 
     switch (Path.GetExtension(path).ToLowerInvariant()) 
     { 
      case ".ps1": 
       enginePath = "powershell.exe"; 
       break; 
      case ".vbs": 
       enginePath = "cscript.exe"; 
       break; 
      default: 
       throw new ApplicationException("Unknown script type"); 
     } 

     string scriptPath = path; 
     Process process = new Process(); 
     process.StartInfo.FileName = enginePath; 
     process.StartInfo.Arguments = string.Format("{0} {1}", scriptPath, parameter); 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.Start(); 
     Console.WriteLine(process.StandardOutput.ReadToEnd()); 
     Console.ReadKey(); 
    } 
+0

:我想要傳遞字符串到腳本,我怎麼能這樣做? – sailer

+1

只是追加你的字符串到'process.StartInfo。參數',編輯了代碼。 –

+0

是的,工作感謝! – sailer

1

試試這個代碼,而不是:

string path = @"C:\mypsscript.bat"; 
Process p = new Process(); 
p.StartInfo.FileName = path; 
p.StartInfo.Arguments = "xyz"; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.Start(); 
Console.WriteLine(p.StandardOutput.ReadToEnd()); 
Console.ReadKey(); 

對於我創建的代碼的批處理文件調試的緣故:

echo %1 

當我運行我得到的上述代碼:

xyz 

因此,這似乎工作正常。嘗試使用像這樣的批處理文件,看看它是否工作,如果它可能是與PowerShell腳本不能正常工作的聯繫,我們稍後可以修復。

+0

@Bail C:我得到錯誤,當我使用P1.StartInfo.FileName = @「C:\ mypsscript.ps1」。錯誤:指定的可執行文件不是有效的Win32應用程序。 – sailer

+0

我得到這個錯誤只是因爲「p.StartInfo.UseShellExecute = false;」指令,無論我是否想重定向輸出。 – sailer

+0

@sailer在發佈代碼後,我重寫了我的答案,請嘗試執行一些調試。 –