Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 956
  • Last Modified:

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

0
VMthinker
Asked:
VMthinker
  • 2
1 Solution
 
Dennis AriesCEO @ Arkro ITCommented:
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.
0
 
Dennis AriesCEO @ Arkro ITCommented:
Forgot to attach the complete code :)
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";
           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();  
           System.IO.File.WriteAllText("C:\\temp\\bin\\ntfs.bodyfile", sRes);
       }   
   }
}

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now