2015-04-15 95 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里面会让它在外面不可用。更正的代码 –