0

How Can i save the CMD Commands into txt file in C#

or how can i display command prompt in C#

here is my code

                      private void button1_Click(object sender, EventArgs e)
    {
        var p = new Process();

        string path = @"C:\Users\Microsoft";
        string argu = "-na>somefile.bat";
        ProcessStartInfo process = new ProcessStartInfo("netstat", argu);
        process.RedirectStandardOutput = false;
        process.UseShellExecute = false;
        process.CreateNoWindow = false;

        Process.Start(process);

        p.StartInfo.WorkingDirectory = path;
        p.StartInfo.FileName = "sr.txt";
        p.Start();
        p.WaitForExit();
    }
user3532929
  • 57
  • 2
  • 9

1 Answers1

2

You can redirect the standard output:

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
    //
    // Setup the process with the ProcessStartInfo class.
    //
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\7za.exe"; // Specify exe name.
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    //
    // Start the process.
    //
    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        using (StreamReader reader = process.StandardOutput)
        {
        string result = reader.ReadToEnd();
        Console.Write(result);
        }
    }
    }
}

Code is from here

Also look to this answer: redirecting output to the text file c#

Community
  • 1
  • 1
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • thanx alot , but first of all i want save the result into txt file then show that 1!! – user3532929 Nov 02 '14 at 21:25
  • @user3532929 than have a look at the second link i posted: http://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp/16256623#16256623 – BendEg Nov 02 '14 at 21:33