VMthinker
asked on
How to use C# Stream Reader/Writer to save a file from a CMD program?
Hi there! I have a program which uses a CMD program to execute various arguements.
The arguements that would work in a normal CMD would be C:\\temp\\bin\\fls.exe -m C: -r C:\\temp\\image.dd > C:\\temp\\bin\\ntfs.bodyfi le .
The C# program that I have written only works till C:\\temp\\bin\\fls.exe -m C: -r C:\\temp\\image.dd but does not execute the > C:\\temp\\bin\\ntfs.bodyfi le which means save the process into the ntfs.bodyfile.
I have read many other websites and they all point to the problem of my program missing a streamreader to save the completion of the process into a file. So can someone please advise on my program to allow the process to be saved into a file? Thanks!
One such similar website that has a same problem would be: http://stackoverflow.com/questions/1521725/streamreader-to-file
The arguements that would work in a normal CMD would be C:\\temp\\bin\\fls.exe -m C: -r C:\\temp\\image.dd > C:\\temp\\bin\\ntfs.bodyfi
The C# program that I have written only works till C:\\temp\\bin\\fls.exe -m C: -r C:\\temp\\image.dd but does not execute the > C:\\temp\\bin\\ntfs.bodyfi
I have read many other websites and they all point to the problem of my program missing a streamreader to save the completion of the process into a file. So can someone please advise on my program to allow the process to be saved into a file? Thanks!
One such similar website that has a same problem would be: http://stackoverflow.com/questions/1521725/streamreader-to-file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process process = new Process();
process.StartInfo.FileName = "C:\\temp\\bin\\fls.exe";
process.StartInfo.Arguments = "-m C: -r C:\\temp\\image.dd > C:\\temp\\bin\\ntfs.bodyfile";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
System.IO.StreamReader reader = process.StandardOutput;
string sRes = reader.ReadToEnd();
Console.WriteLine(sRes);
reader.Close();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After you closed your reader, write your string to your output file
System.IO.File.WriteAllTex
and you are all set.
Basicly, using the streamreader, you read all (standard) output into a string. After finishing the application, you dump all your output into a separate file.