?
Solved

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

Posted on 2010-11-15
2
Medium Priority
?
952 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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