#c# #.net #windows
#c# #.net #Windows
Вопрос:
Здесь у меня есть метод, хотя я хотел бы передать вывод из него в файл, например output.txt как я мог бы это сделать в этом контексте?
foreach (string tmpLine in File.ReadAllLines(@"c:filename.txt"))
{
if (File.Exists(tmpLine))
{
//output
}
}
Ответ №1:
Обычно с точки зрения командной строки вы делаете
mycommand > somefile.txt
или
mycommand | more
Это работает, потому что вывод записывается в std out.
Вы могли бы попробовать http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
Ответ №2:
Вот и все:
var file = File.AppendText(@"c:output.txt");
foreach (string tmpLine in File.ReadAllLines(@"c:filename.txt"))
{
if (File.Exists(tmpLine))
{
file.WriteLine(tmpLine);
}
}
file.Close();
Комментарии:
1. Лучше, конечно, было бы использовать
using
блок наfile
.