2015-04-15 53 views
1

我有一個答案集程序(驗證工作),我想在C#控制檯應用程序中運行。我想將該程序的輸出重定向到一個文本文件,然後我可以讀取它。我想:重定向子程序輸出到一個文件

string directory = @"C:\...\Clingo"; 
string clingoPath = "clingo.exe"; 
string inputPath = "Assignment2.lp"; 
string dataPath = "Data1.lp"; 
string outputPath = "result.txt"; 
string arguments = String.Format("1 \"{0}\" \"{1}\" >\"{2}\"", inputPath, dataPath, outputPath); 

// Set the necessary properties for the process 
ProcessStartInfo clingoProcessInfo = new ProcessStartInfo(clingoPath, arguments); 
clingoProcessInfo.WorkingDirectory = directory; 
clingoProcessInfo.UseShellExecute = false; 

// Start the process 
Process clingoProcess = Process.Start(clingoProcessInfo); 

然而,在運行時,我得到以下錯誤: 「***錯誤:(clingo): '>的Result.txt':無法打開輸入文件」

我想知道如何解決這個問題。

+1

我創建了一個類來處理您的問題。檢查這個問題http://stackoverflow.com/questions/21848271/redirecting-standard-input-of-console-application/21848564#21848564 –

回答

0
using System.IO; 
if (!File.Exists(outputPath)) 
{ 
    FileStream fs = new FileStream(outputPath, FileMode.CreateNew); 
} 
//now do the rest of your stuff 
+0

初始化fs裏面會讓它在外面不可用。更正的代碼 –