Solved

How to use C# Stream Reader/Writer to save a file from a CMD program?

Posted on 2010-11-15
2
945 Views
Last Modified: 2013-12-17
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
Comment
Question by:VMthinker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 34143073
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
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 500 total points
ID: 34143083
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question