In this example,I will show how to send console output to file in c#
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; namespace SNIPPET_CS { class Program { static void Main(string[] args) { ConsoleOutput(); Console.WriteLine("Console output saved successfully"); Console.ReadLine(); } private static void ConsoleOutput() { const string applicationPath = @"D:\Windows\System32\ipconfig.exe"; //string ApplicationArguments = "-c -x"; // Create a new process object Process processObj = new Process(); // StartInfo contains the startup information of // the new process processObj.StartInfo.FileName = applicationPath; //ProcessObj.StartInfo.Arguments = ApplicationArguments; // These two optional flags ensure that no DOS window // appears processObj.StartInfo.UseShellExecute = false; processObj.StartInfo.CreateNoWindow = true; // If this option is set the DOS window appears again :-/ // ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // This ensures that you get the output from the DOS application processObj.StartInfo.RedirectStandardOutput = true; // Start the process processObj.Start(); // Wait that the process exits processObj.WaitForExit(); // Now read the output of the DOS application string result = processObj.StandardOutput.ReadToEnd(); WriteToFile(result); } private static void WriteToFile(string result) { using (FileStream stream = new FileStream(@"C:\temp\a.txt", FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = Encoding.Default.GetBytes(result); stream.Write(buffer, 0, buffer.Length); stream.Close(); } } } }


No comments:
Post a Comment