Solved

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

Posted on 2010-11-15
2
930 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
  • 2
2 Comments
 
LVL 18

Expert Comment

by:Dennis Aries
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 32
Windows Service with UDP 2 26
Word Template Mail merge with vb.net 4 37
Set form below another form 3 24
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now