将子程序输出重定向到文件

时间:2015-04-15 17:33:47

标签: c# io-redirection clingo

我有一个答案集程序(经验证可以工作),我想在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 个答案:

答案 0 :(得分:0)

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