Link to home
Start Free TrialLog in
Avatar of VMthinker
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.bodyfile .

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.bodyfile 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

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();  
       }   
   }
}

Open in new window

Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Remove the "> C:\\temp\\bin\\ntfs.bodyfile" part from your arguments.

After you closed your reader, write your string to your output file

System.IO.File.WriteAllText("C:\\temp\\bin\\ntfs.bodyfile", sRes);

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.
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial