Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to run a command (from C# ) that looks like this
c:\MyCommand /LOAD >c:\temp\mycommand.txt

This is what I tried but the file is not created.

Process Process = new Process();
Process.StartInfo.Arguments = " >c:\\mycommand.txt";
Process.Start("MyCommand","/LOAD");

does not pipe the output to a file

//also tried the following
//Process.Start("MyCommand /LOAD",">c:\temp\mycommand.txt");
//Process.Start("MyCommand", " /LOAD >c:\temp\mycommand.txt");

Any help is greatly appreciated
Posted

One another method to redirect the output of an executable is shown below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"systeminfo.exe"; // Specify exe name.
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                StreamWriter sw = new StreamWriter(@"C:\textinfo.txt");

                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    sw.Write(result);
                    sw.Close();
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 1:23am    
This is the only fully correct answer (at least as much as I can see from the first glance) so far, my 5.
--SA
Sergey Alexandrovich Kryukov 27-Feb-12 1:25am    
Actually, you failed to note that "C:\textinfo.txt" is not a legitimate file location, and there are no cases where hard-coding of any file path names can be useful. (I won't re-vote anyway. :-)
--SA
Chandrakantt 27-Feb-12 1:36am    
I have just provided the sample. The path should be chosen as per the project requirement. I think from next time I should provide comment on the sample code. Anyway Thank for mentioning it. :) I can improve myself in that way.
Redirection doesn't work if you don't launch it inside a CMD.

So, your command line would look like this:
CMD.exe /C "<fullpathto>\MyCommand.exe /LOAD > C:\Temp\MyCommand.txt"</fullpathto>


Keep in mind that normal users are not allowed to create file in C:\Temp in Windows 7. Use the path returned by the TEMP environment variable instead. It will end up in the Temp folder in the users profile.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 1:26am    
Good point about bad file location. I voted 4 because this is still not a solution.
Basically, the right one (with couple of my notes) is the solution 3, please see.
--SA
C#
Process Process = new Process();
Process.StartInfo.Arguments = " >c:\\mycommand.txt";
Process.Start("MyCommand.exe","/LOAD >c:\temp\mycommand.txt");


try that.

Have you tried executing the command in the command prompt first. If so does it work? Before we start to diagnose if something is wrong with the code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900